Skip to content

Commit

Permalink
Merge pull request #16 from DMTF/AddResourceParsing
Browse files Browse the repository at this point in the history
Add Initial Resource parsing logic
  • Loading branch information
mraineri authored Jul 12, 2018
2 parents 4161c43 + 53fe021 commit 0037916
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(REDFISH_HDR_PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include/redfishPayload.h
${CMAKE_CURRENT_SOURCE_DIR}/include/redpath.h)

file(GLOB REDFISH_SRC src/*.c)
file(GLOB REDFISH_SRC src/*.c src/entities/*.c)

source_group("Library Sources" FILES ${REDFISH_SRC})

Expand Down
157 changes: 156 additions & 1 deletion examples/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,28 @@ static struct option long_options[] =
{"session", no_argument, 0, 'S'},
{"verbose", no_argument, 0, 'v'},
{"token", required_argument, 0, 'T'},
{"command", required_argument, 0, 'c'},
{0, 0, 0, 0}
};

typedef void (*commandFunc)(redfishPayload* payload);

typedef struct {
const char* commandName;
commandFunc function;
} commandMapping;

static void getHealth(redfishPayload* payload);
static void getRollup(redfishPayload* payload);
static void getState(redfishPayload* payload);

static commandMapping commands[] = {
{"getHealth", getHealth},
{"getRollup", getRollup},
{"getState", getState},
{NULL, NULL}
};

void inthand(int signum)
{
(void)signum;
Expand Down Expand Up @@ -84,6 +103,7 @@ void print_usage(const char* name)
printf(" -u, --username [user] The username to authenticate with\n");
printf(" -p, --password [pass] The password to authenticate with\n");
printf(" -S, --session Use session based auth, as opposed to basic auth\n");
printf(" -c, --command [command] Run the specified command on the resource\n");
printf("\nQuery:\n");
printf(" Optional: /vXX - Where XX is the version to use. Defaults to v1.\n");
printf(" /Name - Where Name is the name of a JSON tag. If it contains an odata.id only\n");
Expand Down Expand Up @@ -182,6 +202,7 @@ typedef struct
redfishService* redfish;
int argc;
char** argv;
commandMapping* command;
} gotPayloadContext;

void gotPayload(bool success, unsigned short httpCode, redfishPayload* payload, void* context)
Expand All @@ -198,6 +219,13 @@ void gotPayload(bool success, unsigned short httpCode, redfishPayload* payload,
}
if(payload)
{
if(myContext->command)
{
myContext->command->function(payload);
cleanupPayload(payload);
free(context);
return;
}
switch(myContext->method)
{
case 0:
Expand Down Expand Up @@ -254,6 +282,19 @@ void gotPayload(bool success, unsigned short httpCode, redfishPayload* payload,
free(context);
}

static commandMapping* getCommandByString(const char* name)
{
size_t i;
for(i = 0; commands[i].commandName; i++)
{
if(strcasecmp(name, commands[i].commandName) == 0)
{
return &(commands[i]);
}
}
return NULL;
}

int main(int argc, char** argv)
{
int arg;
Expand All @@ -271,10 +312,11 @@ int main(int argc, char** argv)
char* token = NULL;
enumeratorAuthentication auth;
gotPayloadContext* context;
commandMapping* command = NULL;

memset(&auth, 0, sizeof(auth));

while((arg = getopt_long(argc, argv, "?VSH:M:f:W:u:p:vT:", long_options, &opt_index)) != -1)
while((arg = getopt_long(argc, argv, "?VSH:M:f:W:u:p:vT:c:", long_options, &opt_index)) != -1)
{
switch(arg)
{
Expand Down Expand Up @@ -340,6 +382,9 @@ int main(int argc, char** argv)
case 'v':
verbose++;
break;
case 'c':
command = getCommandByString(optarg);
break;
}
}
if(host == NULL)
Expand Down Expand Up @@ -427,6 +472,7 @@ int main(int argc, char** argv)
context->redfish = redfish;
context->argc = argc;
context->argv = argv;
context->command = command;
if(query)
{
getPayloadByPathAsync(redfish, query, NULL, gotPayload, context);
Expand All @@ -452,4 +498,113 @@ static void safeFree(void* ptr)
}
}

static void printHealth(redfishHealth health, const char* healthType)
{
const char* healthStr;
switch(health)
{
case RedfishHealthError:
healthStr = "Error";
break;
case RedfishHealthUnknown:
healthStr = "Unknown";
break;
case RedfishHealthOK:
healthStr = "OK";
break;
case RedfishHealthWarning:
healthStr = "Warning";
break;
case RedfishHealthCritical:
healthStr = "Critical";
break;
default:
healthStr = "Non-enum value";
break;
}
printf("Resource %s is %s (%d)\n", healthType, healthStr, health);
}

static void getHealth(redfishPayload* payload)
{
redfishHealth health;
if(payload == NULL)
{
fprintf(stderr, "Payload is NULL!\n");
return;
}
health = getResourceHealth(payload);
printHealth(health, "health");
}

static void getRollup(redfishPayload* payload)
{
redfishHealth health;
if(payload == NULL)
{
fprintf(stderr, "Payload is NULL!\n");
return;
}
health = getResourceRollupHealth(payload);
printHealth(health, "rollup health");
}

static void getState(redfishPayload* payload)
{
redfishState state;
const char* stateStr;
if(payload == NULL)
{
fprintf(stderr, "Payload is NULL!\n");
return;
}
state = getResourceState(payload);
switch(state)
{
case RedfishStateError:
stateStr = "Error";
break;
case RedfishStateUnknown:
stateStr = "Unknown";
break;
case RedfishStateEnabled:
stateStr = "Enabled";
break;
case RedfishStateDisabled:
stateStr = "Disabled";
break;
case RedfishStateStandbyOffline:
stateStr = "StandbyOffline";
break;
case RedfishStateStandbySpare:
stateStr = "StandbySpare";
break;
case RedfishStateInTest:
stateStr = "InTest";
break;
case RedfishStateStarting:
stateStr = "Starting";
break;
case RedfishStateAbsent:
stateStr = "Absent";
break;
case RedfishStateUnavailableOffline:
stateStr = "UnavailableOffline";
break;
case RedfishStateDeferring:
stateStr = "Deferring";
break;
case RedfishStateQuiesced:
stateStr = "Quiesced";
break;
case RedfishStateUpdating:
stateStr = "Updating";
break;
default:
stateStr = "Non-enum value";
break;
}
printf("Resource state is %s (%d)\n", stateStr, state);
}

/* vim: set tabstop=4 shiftwidth=4 expandtab: */
40 changes: 40 additions & 0 deletions include/entities/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//----------------------------------------------------------------------------
// Copyright Notice:
// Copyright 2017 Distributed Management Task Force, Inc. All rights reserved.
// License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libredfish/blob/master/LICENSE.md
//----------------------------------------------------------------------------
#ifndef _REDFISH_RESOURCE_H_
#define _REDFISH_RESOURCE_H_

//redfishPayload is defined here...
#include "redfishPayload.h"

typedef enum {
RedfishHealthError = -1,
RedfishHealthUnknown = 0,
RedfishHealthOK = 1,
RedfishHealthWarning = 2,
RedfishHealthCritical = 3
} redfishHealth;

typedef enum {
RedfishStateError = -1,
RedfishStateUnknown = 0,
RedfishStateEnabled = 1,
RedfishStateDisabled = 2,
RedfishStateStandbyOffline = 3,
RedfishStateStandbySpare = 4,
RedfishStateInTest = 5,
RedfishStateStarting = 6,
RedfishStateAbsent = 7,
RedfishStateUnavailableOffline = 8,
RedfishStateDeferring = 9,
RedfishStateQuiesced = 10,
RedfishStateUpdating = 11
} redfishState;

REDFISH_EXPORT redfishHealth getResourceHealth(redfishPayload* payload);
REDFISH_EXPORT redfishHealth getResourceRollupHealth(redfishPayload* payload);
REDFISH_EXPORT redfishState getResourceState(redfishPayload* payload);

#endif
1 change: 1 addition & 0 deletions include/redfish.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <redfishService.h>
#include <redfishPayload.h>
#include <redpath.h>
#include <entities/resource.h>

/**
* syslog style function used to debug libredfish.
Expand Down
2 changes: 2 additions & 0 deletions include/redfishPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ REDFISH_EXPORT bool getPayloadByNodeNameAsync(redfishPayload* payload
REDFISH_EXPORT bool getPayloadByIndexAsync(redfishPayload* payload, size_t index, redfishAsyncOptions* options, redfishAsyncCallback callback, void* context);
REDFISH_EXPORT bool getPayloadForPathAsync(redfishPayload* payload, redPathNode* redpath, redfishAsyncOptions* options, redfishAsyncCallback callback, void* context);

REDFISH_EXPORT redfishPayload* getPayloadByNodeNameNoNetwork(redfishPayload* payload, const char* nodeName);

#endif
Loading

0 comments on commit 0037916

Please sign in to comment.