6
/* metadata is present in every type of nc request */
7
ncMetadata * allocate_metadata (char *correlationId, char *userId)
11
if (!(meta = malloc(sizeof(ncMetadata)))) return NULL;
12
if (correlationId) meta->correlationId = strdup(correlationId);
13
if (userId) meta->userId = strdup(userId);
18
void free_metadata (ncMetadata ** metap)
20
ncMetadata * meta = * metap;
21
if (meta->correlationId) free(meta->correlationId);
22
if (meta->userId) free(meta->userId);
26
/* instances are present in instance-related requests */
27
ncInstance * allocate_instance (char *instanceId, char *reservationId,
29
char *imageId, char *imageURL,
30
char *kernelId, char *kernelURL,
31
char *ramdiskId, char *ramdiskURL,
32
char *stateName, int stateCode, char *userId,
33
ncNetConf *ncnet, char *keyName,
34
char *userData, char *launchIndex, char **groupNames, int groupNamesSize)
38
/* zeroed out for cleaner-looking checkpoints and
39
* strings that are empty unless set */
40
if (!(inst = calloc(1, sizeof(ncInstance)))) return NULL;
43
strncpy(inst->userData, userData, CHAR_BUFFER_SIZE*10);
47
strncpy(inst->launchIndex, launchIndex, CHAR_BUFFER_SIZE);
50
inst->groupNamesSize = groupNamesSize;
51
if (groupNames && groupNamesSize) {
53
for (i=0; groupNames[i] && i<groupNamesSize; i++) {
54
strncpy(inst->groupNames[i], groupNames[i], CHAR_BUFFER_SIZE);
57
inst->volumesSize = 0;
60
memcpy(&(inst->ncnet), ncnet, sizeof(ncNetConf));
64
strncpy(inst->instanceId, instanceId, CHAR_BUFFER_SIZE);
68
strncpy(inst->keyName, keyName, CHAR_BUFFER_SIZE*4);
72
strncpy(inst->reservationId, reservationId, CHAR_BUFFER_SIZE);
76
strncpy(inst->imageId, imageId, CHAR_BUFFER_SIZE);
79
strncpy(inst->imageURL, imageURL, CHAR_BUFFER_SIZE);
82
strncpy(inst->kernelId, kernelId, CHAR_BUFFER_SIZE);
85
strncpy(inst->kernelURL, kernelURL, CHAR_BUFFER_SIZE);
88
strncpy(inst->ramdiskId, ramdiskId, CHAR_BUFFER_SIZE);
91
strncpy(inst->ramdiskURL, ramdiskURL, CHAR_BUFFER_SIZE);
95
strncpy(inst->stateName, stateName, CHAR_BUFFER_SIZE);
98
strncpy(inst->userId, userId, CHAR_BUFFER_SIZE);
101
inst->params.memorySize = params->memorySize;
102
inst->params.diskSize = params->diskSize;
103
inst->params.numberOfCores = params->numberOfCores;
105
inst->stateCode = stateCode;
110
void free_instance (ncInstance ** instp)
122
/* resource is used to return information about resources */
123
ncResource * allocate_resource (char *nodeStatus,
124
int memorySizeMax, int memorySizeAvailable,
125
int diskSizeMax, int diskSizeAvailable,
126
int numberOfCoresMax, int numberOfCoresAvailable,
131
if (!(res = malloc(sizeof(ncResource)))) return NULL;
133
strncpy(res->nodeStatus, nodeStatus, CHAR_BUFFER_SIZE);
136
strncpy(res->publicSubnets, publicSubnets, CHAR_BUFFER_SIZE);
138
res->memorySizeMax = memorySizeMax;
139
res->memorySizeAvailable = memorySizeAvailable;
140
res->diskSizeMax = diskSizeMax;
141
res->diskSizeAvailable = diskSizeAvailable;
142
res->numberOfCoresMax = numberOfCoresMax;
143
res->numberOfCoresAvailable = numberOfCoresAvailable;
145
if (!res->nodeStatus ) free_resource(&res);
149
void free_resource (ncResource ** resp)
161
instance_error_codes add_instance (bunchOfInstances ** headp, ncInstance * instance)
163
bunchOfInstances * new = malloc (sizeof(bunchOfInstances));
165
if ( new == NULL ) return OUT_OF_MEMORY;
166
new->instance = instance;
169
if ( * headp == NULL) {
171
(* headp)->count = 1;
174
bunchOfInstances * last = * headp;
176
if ( !strcmp(last->instance->instanceId, instance->instanceId) ) {
180
} while ( last->next && (last = last->next) );
188
instance_error_codes remove_instance (bunchOfInstances ** headp, ncInstance * instance)
190
bunchOfInstances * head, * prev = NULL;
192
for ( head = * headp; head; ) {
193
int count = (* headp)->count;
195
if ( !strcmp(head->instance->instanceId, instance->instanceId) ) {
197
prev->next = head->next;
199
* headp = head->next;
202
(* headp)->count = count - 1;
213
instance_error_codes for_each_instance (bunchOfInstances ** headp, void (* function)(bunchOfInstances **, ncInstance *, void *), void *param)
215
bunchOfInstances * head;
217
for ( head = * headp; head; head = head->next ) {
218
function ( headp, head->instance, param );
224
ncInstance * find_instance (bunchOfInstances **headp, char * instanceId)
226
bunchOfInstances * head;
228
for ( head = * headp; head; head = head->next ) {
229
if (!strcmp(head->instance->instanceId, instanceId)) {
230
return head->instance;
237
ncInstance * get_instance (bunchOfInstances **headp)
239
static bunchOfInstances * current = NULL;
241
/* advance static variable, wrapping to head if at the end */
242
if ( current == NULL ) current = * headp;
243
else current = current->next;
245
/* return the new value, if any */
246
if ( current == NULL ) return NULL;
247
else return current->instance;
250
int total_instances (bunchOfInstances **headp)
252
if ( * headp ) return (* headp)->count;
257
* finds a matching volume
258
* OR returns a pointer to the next empty volume slot
259
* OR if full, returns NULL
261
static ncVolume * find_volume (ncInstance * instance, char *volumeId)
263
ncVolume * v = instance->volumes;
266
for (i=0; i<EUCA_MAX_VOLUMES; i++,v++) {
267
if ( ! strncmp (v->volumeId, volumeId, CHAR_BUFFER_SIZE) )
269
if ( ! strnlen (v->volumeId, CHAR_BUFFER_SIZE) )
272
if (i==EUCA_MAX_VOLUMES)
278
ncVolume * add_volume (ncInstance * instance, char *volumeId, char *remoteDev, char *localDev)
280
ncVolume * v = find_volume (instance, volumeId);
283
return NULL; /* out of room */
286
if ( ! strncmp (v->volumeId, volumeId, CHAR_BUFFER_SIZE) ) {
287
return NULL; /* already there */
289
strncpy (v->volumeId, volumeId, CHAR_BUFFER_SIZE);
290
strncpy (v->remoteDev, remoteDev, CHAR_BUFFER_SIZE);
291
strncpy (v->localDev, localDev , CHAR_BUFFER_SIZE);
292
instance->volumesSize++;
298
ncVolume * free_volume (ncInstance * instance, char *volumeId, char *remoteDev, char *localDev)
300
ncVolume * v = find_volume (instance, volumeId);
303
return NULL; /* not there (and out of room) */
306
if ( strncmp (v->volumeId, volumeId, CHAR_BUFFER_SIZE) ) {
307
return NULL; /* not there */
310
ncVolume * last_v = instance->volumes+(EUCA_MAX_VOLUMES-1);
311
int slots_left = last_v - v;
312
/* shift the remaining entries up, empty or not */
314
memmove (v, v+1, slots_left*sizeof(ncVolume));
316
/* empty the last one */
317
bzero (last_v, sizeof(ncVolume));
318
instance->volumesSize--;