void * xrealloc (void *p, size_t n) { void *result = realloc (p, n); if (result == NULL && (n > 0 || p == NULL)) oom_error ("realloc", n); return result; }
static int prepare_int_parameter(struct deltacloud_create_parameter *param, const char *name, int value) { param->name = strdup(name); if (param->name == NULL) { oom_error(); return -1; } if (asprintf(¶m->value, "%d", value) < 0) { SAFE_FREE(param->name); oom_error(); return -1; } return 0; }
static int parse_one_blob(xmlNodePtr cur, xmlXPathContextPtr ctxt, void *output) { struct deltacloud_bucket_blob *thisblob = (struct deltacloud_bucket_blob *)output; struct deltacloud_bucket_blob_metadata *thisentry; xmlNodePtr meta_cur, entry_cur, oldnode; int ret = -1; oldnode = ctxt->node; memset(thisblob, 0, sizeof(struct deltacloud_bucket_blob)); thisblob->href = getXPathString("string(./@href)", ctxt); thisblob->id = getXPathString("string(./@id)", ctxt); thisblob->bucket_id = getXPathString("string(./bucket)", ctxt); thisblob->content_length = getXPathString("string(./content_length)", ctxt); thisblob->content_type = getXPathString("string(./content_type)", ctxt); thisblob->last_modified = getXPathString("string(./last_modified)", ctxt); thisblob->content_href = getXPathString("string(./content/@href)", ctxt); meta_cur = cur->children; while (meta_cur != NULL) { if (meta_cur->type == XML_ELEMENT_NODE && STREQ((const char *)meta_cur->name, "user_metadata")) { entry_cur = meta_cur->children; while (entry_cur != NULL) { if (entry_cur->type == XML_ELEMENT_NODE && STREQ((const char *)entry_cur->name, "entry")) { ctxt->node = entry_cur; thisentry = calloc(1, sizeof(struct deltacloud_bucket_blob_metadata)); if (thisentry == NULL) { deltacloud_free_bucket_blob(thisblob); oom_error(); goto cleanup; } thisentry->key = getXPathString("string(./@key)", ctxt); thisentry->value = getXPathString("string(.)", ctxt); if (thisentry->value != NULL) { strip_trailing_whitespace(thisentry->value); strip_leading_whitespace(thisentry->value); } /* add_to_list can't fail */ add_to_list(&thisblob->metadata, struct deltacloud_bucket_blob_metadata, thisentry); } entry_cur = entry_cur->next; } }
int parse_addresses_xml(xmlNodePtr root, xmlXPathContextPtr ctxt, struct deltacloud_address **addresses) { struct deltacloud_address *thisaddr; char *address; xmlNodePtr oldnode, cur; int ret = -1; *addresses = NULL; oldnode = ctxt->node; ctxt->node = root; cur = root->children; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE && STREQ((const char *)cur->name, "address")) { address = getXPathString("string(./address)", ctxt); if (address != NULL) { thisaddr = calloc(1, sizeof(struct deltacloud_address)); if (thisaddr == NULL) { SAFE_FREE(address); oom_error(); goto cleanup; } thisaddr->address = address; /* add_to_list can't fail */ add_to_list(addresses, struct deltacloud_address, thisaddr); } /* address is allowed to be NULL, so skip it here */ } cur = cur->next; } ret = 0; cleanup: ctxt->node = oldnode; if (ret < 0) free_address_list(addresses); return ret; }
static int parse_lb_instance_xml(xmlNodePtr root, xmlXPathContextPtr ctxt, struct deltacloud_loadbalancer_instance **instances) { struct deltacloud_loadbalancer_instance *thisinst; xmlNodePtr oldnode, cur; int ret = -1; *instances = NULL; oldnode = ctxt->node; ctxt->node = root; cur = root->children; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE && STREQ((const char *)cur->name, "instance")) { thisinst = calloc(1, sizeof(struct deltacloud_loadbalancer_instance)); if (thisinst == NULL) { oom_error(); goto cleanup; } thisinst->href = (char *)xmlGetProp(cur, BAD_CAST "href"); thisinst->id = (char *)xmlGetProp(cur, BAD_CAST "id"); if (parse_link_xml(cur->children, &(thisinst->links)) < 0) { free_lb_instance(thisinst); SAFE_FREE(thisinst); goto cleanup; } /* add_to_list can't fail */ add_to_list(instances, struct deltacloud_loadbalancer_instance, thisinst); } cur = cur->next; } ret = 0; cleanup: ctxt->node = oldnode; return ret; }
static int parse_storage_snapshot_xml(xmlNodePtr cur, xmlXPathContextPtr ctxt, void **data) { struct deltacloud_storage_snapshot **storage_snapshots = (struct deltacloud_storage_snapshot **)data; struct deltacloud_storage_snapshot *thissnapshot; int ret = -1; xmlNodePtr oldnode; oldnode = ctxt->node; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE && STREQ((const char *)cur->name, "storage_snapshot")) { ctxt->node = cur; thissnapshot = calloc(1, sizeof(struct deltacloud_storage_snapshot)); if (thissnapshot == NULL) { oom_error(); goto cleanup; } if (parse_one_storage_snapshot(cur, ctxt, thissnapshot) < 0) { /* parse_one_storage_snapshot is expected to have set its own error */ SAFE_FREE(thissnapshot); goto cleanup; } /* add_to_list can't fail */ add_to_list(storage_snapshots, struct deltacloud_storage_snapshot, thissnapshot); } cur = cur->next; } ret = 0; cleanup: ctxt->node = oldnode; if (ret < 0) deltacloud_free_storage_snapshot_list(storage_snapshots); return ret; }
static int parse_listener_xml(xmlNodePtr root, xmlXPathContextPtr ctxt, struct deltacloud_loadbalancer_listener **listeners) { struct deltacloud_loadbalancer_listener *thislistener; xmlNodePtr oldnode, cur; int ret = -1; *listeners = NULL; oldnode = ctxt->node; ctxt->node = root; cur = root->children; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE && STREQ((const char *)cur->name, "listener")) { thislistener = calloc(1, sizeof(struct deltacloud_loadbalancer_listener)); if (thislistener == NULL) { oom_error(); goto cleanup; } thislistener->protocol = (char *)xmlGetProp(cur, BAD_CAST "protocol"); thislistener->load_balancer_port = getXPathString("string(./listener/load_balancer_port)", ctxt); thislistener->instance_port = getXPathString("string(./listener/instance_port)", ctxt); /* add_to_list can't fail */ add_to_list(listeners, struct deltacloud_loadbalancer_listener, thislistener); } cur = cur->next; } ret = 0; cleanup: ctxt->node = oldnode; return ret; }
static int parse_loadbalancer_xml(xmlNodePtr cur, xmlXPathContextPtr ctxt, void **data) { struct deltacloud_loadbalancer **lbs = (struct deltacloud_loadbalancer **)data; struct deltacloud_loadbalancer *thislb; int ret = -1; xmlNodePtr oldnode; oldnode = ctxt->node; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE && STREQ((const char *)cur->name, "load_balancer")) { ctxt->node = cur; thislb = calloc(1, sizeof(struct deltacloud_loadbalancer)); if (thislb == NULL) { oom_error(); goto cleanup; } if (parse_one_loadbalancer(cur, ctxt, thislb) < 0) { /* parse_one_loadbalancer is expected to have set its own error */ SAFE_FREE(thislb); goto cleanup; } /* add_to_list can't fail */ add_to_list(lbs, struct deltacloud_loadbalancer, thislb); } cur = cur->next; } ret = 0; cleanup: ctxt->node = oldnode; if (ret < 0) deltacloud_free_loadbalancer_list(lbs); return ret; }
int parse_metric_xml(xmlNodePtr cur, xmlXPathContextPtr ctxt, void **data) { struct deltacloud_metric **metrics = (struct deltacloud_metric **)data; struct deltacloud_metric *thismetric; xmlNodePtr oldnode; int ret = -1; *metrics = NULL; oldnode = ctxt->node; cur = cur->children->next->next->next->children; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE) { thismetric = calloc(1, sizeof(struct deltacloud_metric)); thismetric->name = strdup((const char *)cur->name); ctxt->node = cur; if (thismetric == NULL) { oom_error(); goto cleanup; } if (parse_metric_value_xml(cur, ctxt, thismetric) < 0) { /* parse_one_instance is expected to have set its own error */ SAFE_FREE(thismetric); goto cleanup; } /* add_to_list can't fail */ add_to_list(metrics, struct deltacloud_metric, thismetric); } cur = cur->next; } ret = 0; cleanup: if (ret < 0) deltacloud_free_metric_list(metrics); ctxt->node = oldnode; return ret; }
/** * A function to create a new load balancer. * @param[in] api The deltacloud_api structure representing the connection * @param[in] name The name to give to the new load balancer * @param[in] realm_id The realm ID to put the new load balancer in * @param[in] protocol The protocol to load balance * @param[in] balancer_port The port the load balancer listens on * @param[in] instance_port The port the load balancer balances to * @param[in] params An array of deltacloud_create_parameter structures that * represent any optional parameters to pass into the * create call * @param[in] params_length An integer describing the length of the params * array * @returns 0 on success, -1 on error */ int deltacloud_create_loadbalancer(struct deltacloud_api *api, const char *name, const char *realm_id, const char *protocol, int balancer_port, int instance_port, struct deltacloud_create_parameter *params, int params_length) { struct deltacloud_create_parameter *internal_params; int ret = -1; int pos; if (!valid_api(api) || !valid_arg(name) || !valid_arg(realm_id) || !valid_arg(protocol)) return -1; if (balancer_port < 0 || balancer_port > 65536) { invalid_argument_error("balancer_port must be between 0 and 65536"); return -1; } if (instance_port < 0 || instance_port > 65536) { invalid_argument_error("instance_port must be between 0 and 65536"); return -1; } if (params_length < 0) { invalid_argument_error("params_length must be >= 0"); return -1; } internal_params = calloc(params_length + 5, sizeof(struct deltacloud_create_parameter)); if (internal_params == NULL) { oom_error(); return -1; } pos = copy_parameters(internal_params, params, params_length); if (pos < 0) /* copy_parameters already set the error */ goto cleanup; if (deltacloud_prepare_parameter(&internal_params[pos++], "name", name) < 0 || deltacloud_prepare_parameter(&internal_params[pos++], "realm_id", realm_id) < 0 || deltacloud_prepare_parameter(&internal_params[pos++], "listener_protocol", protocol) < 0) /* deltacloud_prepare_parameter already set the error */ goto cleanup; if (prepare_int_parameter(&internal_params[pos++], "listener_balancer_port", balancer_port) < 0) /* prepare_int_parameter already set the error */ goto cleanup; if (prepare_int_parameter(&internal_params[pos++], "listener_instance_port", instance_port) < 0) /* prepare_int_parameter already set the error */ goto cleanup; if (internal_create(api, "load_balancers", internal_params, pos, NULL, NULL) < 0) /* internal_create already set the error */ goto cleanup; ret = 0; cleanup: free_parameters(internal_params, pos); SAFE_FREE(internal_params); return ret; }
static int lb_register_unregister(struct deltacloud_api *api, struct deltacloud_loadbalancer *balancer, const char *instance_id, struct deltacloud_create_parameter *params, int params_length, const char *link) { struct deltacloud_link *thislink; struct deltacloud_create_parameter *internal_params; char *href; int ret = -1; int pos; int rc; if (!valid_api(api) || !valid_arg(balancer) || !valid_arg(instance_id)) return -1; if (params_length < 0) { invalid_argument_error("params_length must be >= 0"); return -1; } thislink = api_find_link(api, "load_balancers"); if (thislink == NULL) /* api_find_link set the error */ return -1; internal_params = calloc(params_length + 1, sizeof(struct deltacloud_create_parameter)); if (internal_params == NULL) { oom_error(); return -1; } pos = copy_parameters(internal_params, params, params_length); if (pos < 0) /* copy_parameters already set the error */ goto cleanup; if (deltacloud_prepare_parameter(&internal_params[pos++], "instance_id", instance_id) < 0) /* deltacloud_prepare_parameter already set the error */ goto cleanup; if (asprintf(&href, "%s/%s/%s", thislink->href, balancer->id, link) < 0) { oom_error(); goto cleanup; } rc = internal_post(api, href, internal_params, pos, NULL, NULL); SAFE_FREE(href); if (rc < 0) /* internal_post already set the error */ goto cleanup; ret = 0; cleanup: free_parameters(internal_params, pos); SAFE_FREE(internal_params); return ret; }
/** * A function to create a new storage snapshot. * @param[in] api The deltacloud_api structure representing the connection * @param[in] volume The volume to take the snapshot from * @param[in] params An array of deltacloud_create_parameter structures that * represent any optional parameters to pass into the * create call * @param[in] params_length An integer describing the length of the params * array * @param[out] snap_id The snapshot_id returned by the create call * @returns 0 on success, -1 on error */ int deltacloud_create_storage_snapshot(struct deltacloud_api *api, struct deltacloud_storage_volume *volume, struct deltacloud_create_parameter *params, int params_length, char **snap_id) { struct deltacloud_create_parameter *internal_params; struct deltacloud_storage_snapshot snap; char *data = NULL; int ret = -1; int pos; if (!valid_api(api) || !valid_arg(volume)) return -1; if (params_length < 0) { invalid_argument_error("params_length must be >= 0"); return -1; } internal_params = calloc(params_length + 1, sizeof(struct deltacloud_create_parameter)); if (internal_params == NULL) { oom_error(); return -1; } pos = copy_parameters(internal_params, params, params_length); if (pos < 0) /* copy_parameters already set the error */ goto cleanup; if (deltacloud_prepare_parameter(&internal_params[pos++], "volume_id", volume->id) < 0) /* deltacloud_create_parameter already set the error */ goto cleanup; if (internal_create(api, "storage_snapshots", internal_params, pos, &data, NULL) < 0) /* internal_create already set the error */ goto cleanup; if (snap_id != NULL) { if (internal_xml_parse(data, "storage_snapshot", parse_one_storage_snapshot, 1, &snap) < 0) /* internal_xml_parse set the error */ goto cleanup; *snap_id = strdup(snap.id); deltacloud_free_storage_snapshot(&snap); if (*snap_id == NULL) { oom_error(); goto cleanup; } } ret = 0; cleanup: free_parameters(internal_params, pos); SAFE_FREE(internal_params); SAFE_FREE(data); return ret; }