The Assimilation Project  based on Assimilation version 1.1.7.1474836767
resourcecmd.c
Go to the documentation of this file.
1 
24 #include <projectcommon.h>
25 #include <string.h>
26 #define RESOURCECMD_SUBCLASS
27 #include <resourcecmd.h>
28 #include <resourceocf.h>
29 #include <resourcelsb.h>
30 #include <resourcenagios.h>
31 
33 
39 
44 static const struct {
45  const char * classname;
46  ResourceCmd* (*constructor) (
47  guint structsize
48 , ConfigContext* request
49 , gpointer user_data
50 , ResourceCmdCallback callback);
51 }subclasses[] = {
52  {"ocf", resourceocf_new}, //< OCF resource agents
53  {"lsb", resourcelsb_new}, //< LSB (/etc/init.d) resource agents
54  {"nagios", resourcenagios_new} //< NAGIOS resource agents
55 };
58 
66 , gpointer user_data
67 , ResourceCmdCallback callback)
68 
69 {
70  guint j;
71  const char * cname;
72 
73  if (NULL == request) {
74  g_warning("%s.%d: NULL resourcecmd request" , __FUNCTION__, __LINE__);
75  return NULL;
76  }
77  cname = request->getstring(request, REQCLASSNAMEFIELD);
78 
79  if (NULL == cname) {
80  char * reqstr = request->baseclass.toString(&request->baseclass);
81  g_warning("%s.%d: No class name in request [%s]", __FUNCTION__, __LINE__
82  , reqstr);
83  g_free(reqstr); reqstr = NULL;
84  return NULL;
85  }
86 
87  for (j=0; j < DIMOF(subclasses) && subclasses[j].classname; ++j) {
88  if (strcmp(cname, subclasses[j].classname) == 0) {
89  return subclasses[j].constructor(0, request, user_data, callback);
90  }
91  }
92  g_warning("%s.%d: Invalid resource class [%s]", __FUNCTION__, __LINE__, cname);
93  return NULL;
94 }
95 
97 void
99 {
100  ResourceCmd* self = CASTTOCLASS(ResourceCmd, aself);
101 
102  if (self->request) {
103  UNREF(self->request);
104  }
105  self->user_data = NULL;
106  self->callback = NULL;
107  _assimobj_finalize(aself);
108 }
109 
110 
114  guint structsize
115 , ConfigContext* request
116 , gpointer user_data
117 , ResourceCmdCallback callback)
118 {
119  AssimObj* aself;
120  ResourceCmd* self;
121  const char* rscname;
122  const char* operation;
123 
125  if (structsize < sizeof(ResourceCmd)) {
126  structsize = sizeof(ResourceCmd);
127  }
128  rscname = request->getstring(request, CONFIGNAME_INSTANCE);
129  if (NULL == rscname) {
130  char * reqstr = request->baseclass.toString(&request->baseclass);
131  g_warning("%s.%d: No resource name in request [%s]", __FUNCTION__, __LINE__
132  , reqstr);
133  g_free(reqstr); reqstr = NULL;
134  return NULL;
135  }
136  operation = request->getstring(request, REQOPERATIONNAMEFIELD);
137  if (NULL == operation) {
138  char * reqstr = request->baseclass.toString(&request->baseclass);
139  g_warning("%s.%d: No "REQOPERATIONNAMEFIELD" name in request [%s]"
140  , __FUNCTION__, __LINE__ , reqstr);
141  g_free(reqstr); reqstr = NULL;
142  return NULL;
143  }
144  aself = assimobj_new(structsize);
145  self = NEWSUBCLASS(ResourceCmd, aself);
146 
147  self->request = request;
148  REF(self->request);
149  self->user_data = user_data;
150  self->callback = callback;
151  self->execute = _resourcecmd_execute;
152  self->resourcename = rscname;
153  self->operation = operation;
154 
156  return self;
157 }
159 FSTATIC void
161 {
162  (void)self;
163  g_warning("%s.%d: Abstract class -- cannot execute."
164  , __FUNCTION__, __LINE__);
165 }
void _assimobj_finalize(AssimObj *self)
Definition: assimobj.c:61
void(* ResourceCmdCallback)(ConfigContext *request, gpointer user_data, enum HowDied reason, int rc, int signal, gboolean core_dumped, const char *stringresult)
Definition: resourcecmd.h:44
ResourceCmd * resourcenagios_new(guint structsize, ConfigContext *request, gpointer user_data, ResourceCmdCallback callback)
Constructor for ResourceNAGIOS class.
#define REQOPERATIONNAMEFIELD
Definition: resourcecmd.h:71
#define REQCLASSNAMEFIELD
Definition: resourcecmd.h:69
const char *(* getstring)(const ConfigContext *, const char *name)
Get String value.
Definition: configcontext.h:86
ResourceCmd * resourceocf_new(guint structsize, ConfigContext *request, gpointer user_data, ResourceCmdCallback callback)
Constructor for ResourceOCF class.
Definition: resourceocf.c:49
Implements the NAGIOS resource class.
struct _ResourceCmd ResourceCmd
Definition: resourcecmd.h:48
#define FSTATIC
Definition: projectcommon.h:31
ResourceCmd * resourcecmd_constructor(guint structsize, ConfigContext *request, gpointer user_data, ResourceCmdCallback callback)
Constructor (_new function) for ResourceCmd "abstract" class.
Definition: resourcecmd.c:113
#define BINDDEBUG(Cclass)
BINDDEBUG is for telling the class system where the debug variable for this class is - put it in the ...
Definition: proj_classes.h:82
AssimObj baseclass
Definition: configcontext.h:72
#define __FUNCTION__
#define REF(obj)
Definition: assimobj.h:39
gchar *(* toString)(gconstpointer)
Produce malloc-ed string representation.
Definition: assimobj.h:58
ResourceCmd * resourcelsb_new(guint structsize, ConfigContext *request, gpointer user_data, ResourceCmdCallback callback)
Constructor for ResourceLSB class.
Definition: resourcelsb.c:49
Implements the OCF resource class.
Project common header file.
#define DIMOF(a)
Definition: lldp_min.c:30
AssimObj * assimobj_new(guint objsize)
Definition: assimobj.c:74
const char * classname
Definition: resourcecmd.c:45
Implements the LSB (Linux Standard Base aka SysV Init script) resource class.
#define CONFIGNAME_INSTANCE
Instance name for discovery.
ResourceCmd * resourcecmd_new(ConfigContext *request, gpointer user_data, ResourceCmdCallback callback)
Our ResourceCmd Factory object - constructs an object of the proper subclass for the given instantiat...
Definition: resourcecmd.c:65
#define DEBUGDECLARATIONS
Definition: proj_classes.h:79
Implements the resource command abstract class.
FSTATIC void _resourcecmd_execute(ResourceCmd *self)
Moan and complain - abstract class.
Definition: resourcecmd.c:160
void(* _finalize)(AssimObj *)
Free object (private)
Definition: assimobj.h:55
#define CASTTOCLASS(Cclass, obj)
Safely cast &#39;obj&#39; to C-class &#39;class&#39; - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
#define UNREF(obj)
Definition: assimobj.h:35
FSTATIC void _resourcecmd_finalize(AssimObj *)
Finalize function for ResourceCmd objects.
Definition: resourcecmd.c:98
#define NEWSUBCLASS(Cclass, obj)
Definition: proj_classes.h:67