Beispiel #1
0
void image_id_get(char *image_name,
	void (*completion_func)(char *image_id, void *data),
	void *data)
{
	static struct deltacloud_api api;
	struct deltacloud_image *images_head;
	struct deltacloud_image *images;
	int rc;

	qb_enter();

	if (deltacloud_initialize(&api, "http://localhost:3001/api",
				  "dep-wp", "") < 0) {
		qb_log(LOG_ERR, "Failed to initialize libdeltacloud: %s",
		       deltacloud_get_last_error_string());

		qb_leave();
		return;
	}
	rc = deltacloud_get_images(&api, &images);
	if (rc < 0) {
		qb_log(LOG_ERR, "Failed to initialize libdeltacloud: %s",
		       deltacloud_get_last_error_string());

		qb_leave();
		return;
	}

	for (images_head = images; images; images = images->next) {
		if (strcmp(images->name, image_name) == 0) {
			completion_func(images->id, data);
			break;
		}
	}
	deltacloud_free_image_list(&images_head);
	deltacloud_free(&api);

	qb_leave();
}
int main(int argc, char *argv[])
{
  struct deltacloud_api api;
  struct deltacloud_api zeroapi;
  struct deltacloud_image image;
  struct deltacloud_image *images = NULL;
  //struct deltacloud_instance instance;
  //char *instid;
  //char *imgid;
  //int timeout;
  int ret = 3;

  if (argc != 4) {
    fprintf(stderr, "Usage: %s <url> <user> <password>\n", argv[0]);
    return 1;
  }

  memset(&zeroapi, 0, sizeof(struct deltacloud_api));

  if (deltacloud_initialize(&api, argv[1], argv[2], argv[3]) < 0) {
    fprintf(stderr, "Failed to find links for the API: %s\n",
	    deltacloud_get_last_error_string());
    return 2;
  }

  /* test out deltacloud_supports_images */
  if (deltacloud_supports_images(NULL) >= 0) {
    fprintf(stderr, "Expected deltacloud_supports_images to fail with NULL api, but succeeded\n");
    goto cleanup;
  }

  if (deltacloud_supports_images(&zeroapi) >= 0) {
    fprintf(stderr, "Expected deltacloud_supports_images to fail with uninitialized api, but succeeded\n");
    goto cleanup;
  }

  if (deltacloud_supports_images(&api)) {

    /* test out deltacloud_get_images */
    if (deltacloud_get_images(NULL, &images) >= 0) {
      fprintf(stderr, "Expected deltacloud_get_images to fail with NULL api, but succeeded\n");
      goto cleanup;
    }

    if (deltacloud_get_images(&api, NULL) >= 0) {
      fprintf(stderr, "Expected deltacloud_get_images to fail with NULL images, but succeeded\n");
      goto cleanup;
    }

    if (deltacloud_get_images(&zeroapi, &images) >= 0) {
      fprintf(stderr, "Expected deltacloud_get_images to fail with unintialized api, but succeeded\n");
      goto cleanup;
    }

    if (deltacloud_get_images(&api, &images) < 0) {
      fprintf(stderr, "Failed to get_images: %s\n",
	      deltacloud_get_last_error_string());
      goto cleanup;
    }
    print_image_list(images);

    if (images != NULL) {

      /* test out deltacloud_get_image_by_id */
      if (deltacloud_get_image_by_id(NULL, images->id, &image) >= 0) {
	fprintf(stderr, "Expected deltacloud_get_image_by_id to fail with NULL api, but succeeded\n");
	goto cleanup;
      }

      if (deltacloud_get_image_by_id(&api, NULL, &image) >= 0) {
	fprintf(stderr, "Expected deltacloud_get_image_by_id to fail with NULL id, but succeeded\n");
	goto cleanup;
      }

      if (deltacloud_get_image_by_id(&api, images->id, NULL) >= 0) {
	fprintf(stderr, "Expected deltacloud_get_image_by_id to fail with NULL image, but succeeded\n");
	goto cleanup;
      }

      if (deltacloud_get_image_by_id(&api, "bogus_id", &image) >= 0) {
	fprintf(stderr, "Expected deltacloud_get_image_by_id to fail with bogus id, but succeeded\n");
	goto cleanup;
      }

      if (deltacloud_get_image_by_id(&zeroapi, images->id, &image) >= 0) {
	fprintf(stderr, "Expected deltacloud_get_image_by_id to fail with unintialized api, but succeeded\n");
	goto cleanup;
      }

      /* here we use the first image from the list above */
      if (deltacloud_get_image_by_id(&api, images->id, &image) < 0) {
	fprintf(stderr, "Failed to get image by id: %s\n",
		deltacloud_get_last_error_string());
	goto cleanup;
      }
      print_image(&image);
      deltacloud_free_image(&image);
    }


    /* FIXME: due to bugs in deltacloud, I can't seem to make this work
     * at present.  We'll have to revisit.
     */
#if 0
    if (deltacloud_supports_instances(&api)) {

      /* the first thing we need to do is create an instance that we will
       * snapshot from.  In order to create an instance, we need to find
       * an image to base the instance off of.  We fetch the list of images
       * and just use the first one
       */
      if (deltacloud_get_images(&api, &images) < 0) {
	fprintf(stderr, "Failed to get images: %s\n",
		deltacloud_get_last_error_string());
	goto cleanup;
      }

      if (deltacloud_create_instance(&api, images->id, NULL, 0, &instid) < 0) {
	deltacloud_free_image_list(&images);
	fprintf(stderr, "Failed to create instance: %s\n",
		deltacloud_get_last_error_string());
	goto cleanup;
      }

      deltacloud_free_image_list(&images);

      if (wait_for_instance_boot(api, instid, &instance) != 1) {
	free(instid);
	goto cleanup;
      }

      free(instid);

      /* if we made it here, then the instance went to running.  We can
       * now do operations on it
       */
      if (deltacloud_create_image(&api, "newimage", &instance, NULL, 0,
				  &imgid) < 0) {
	deltacloud_instance_destroy(&api, &instance);
	deltacloud_free_instance(&instance);
	fprintf(stderr, "Failed to create image from instance: %s\n",
		deltacloud_get_last_error_string());
	goto cleanup;
      }

      free(imgid);
      deltacloud_instance_destroy(&api, &instance);
      deltacloud_free_instance(&instance);
    }
    else
      fprintf(stderr, "Instances are not supported, so image creation test is skipped\n");
#endif
  }
  else
    fprintf(stderr, "Images are not supported\n");

  ret = 0;

 cleanup:
  deltacloud_free_image_list(&images);

  deltacloud_free(&api);

  return ret;
}