123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- #include <curl/curl.h>
- #include "cJSON.h"
- #include "curl.h"
- #include "compute.h"
- #include "keystone.h"
- #include "utils.h"
- int get_compute_aggregates(struct string *s) {
- char ept[2048] = { 0 };
- get_interface_endpoint(ept, "compute");
- return submit_api_request(s, ept, "/os-aggregates");
- }
- static int list(int argc, char **argv) {
- struct string s;
- int err = get_compute_aggregates(&s);
- cJSON *response = cJSON_Parse(s.ptr);
- cJSON *aggregates = cJSON_GetObjectItem(response, "aggregates");
- int n_aggs = cJSON_GetArraySize(aggregates);
- enum field {
- id, name, az, n_fields
- };
- char aggregate_info[n_aggs][n_fields][MAX_BUF_SZ];
- char *field_name[n_fields] = { "id", "name", "availability_zone" };
- enum field this_width[n_fields] = { 0, 0, 0 };
- enum field max_width[n_fields] = { 0, 0, 0 };
- int agg_ctr = 0;
- cJSON *a;
- cJSON_ArrayForEach(a, aggregates) {
- cJSON *obj[n_fields];
- for(enum field fld = 0; fld < n_fields; ++fld) {
- obj[fld] = cJSON_GetObjectItem(a, field_name[fld]);
- switch(obj[fld]->type) {
- case cJSON_Number:
- this_width[fld]= strnlen(cJSON_Print(obj[fld]), MAX_BUF_SZ);
- sprintf(aggregate_info[agg_ctr][fld], "%d", obj[fld]->valueint);
- case cJSON_String:
- if(obj[fld]->valuestring != NULL) {
- strncpy(aggregate_info[agg_ctr][fld], cJSON_GetStringValue(obj[fld]), MAX_BUF_SZ);
- this_width[fld]= strnlen(aggregate_info[agg_ctr][fld], MAX_BUF_SZ);
- }
- }
- if(this_width[fld] > max_width[fld]) {
- max_width[fld] = this_width[fld];
- }
- }
- // We need to free the objects in a separate loop or else get a segfault :shrug:
- for(enum field fld = 0; fld < n_fields; ++fld) {
- if(obj[fld] != NULL) {
- cJSON_free(obj[fld]);
- }
- }
- ++agg_ctr;
- }
- for(agg_ctr = 0; agg_ctr < n_aggs; ++agg_ctr) {
- for(enum field fld = 0; fld < n_fields; ++fld) {
- printf("%s%*s", fld == 0 ? "" : " ", max_width[fld], aggregate_info[agg_ctr][fld]);
- }
- printf("\n");
- }
- if(a != NULL) {
- free(a);
- }
- if(aggregates != NULL) {
- free(aggregates);
- }
- if(response != NULL) {
- free(response);
- }
- if(s.ptr != NULL) {
- free(s.ptr);
- }
- return err;
- }
- static struct command subcommands[] = {
- CMD_DEF(list),
- { NULL, NULL }
- };
- int aggregate(int argc, char **argv) {
- common_main(argc, argv, subcommands);
- return 0;
- }
|