示例#1
0
def terminate_domain_instance(username, domain_id, instance_id):
    user_obj = get_user_object(username)
    instance_to_terminate = get_domain_instance(username, domain_id, instance_id)
    if instance_to_terminate is None:
        raise PhantomWebException("No instance %s available to terminate" % instance_id)

    instance_iaas_id = instance_to_terminate.get('iaas_instance_id')
    if instance_iaas_id is None:
        raise PhantomWebException("Instance %s has no iaas ID" % instance_id)

    cloud_name = instance_to_terminate.get('cloud')
    cloud_name = cloud_name.split("/")[-1]

    iaas_cloud = user_obj.get_cloud(cloud_name)
    iaas_connection = iaas_cloud.get_iaas_compute_con()

    log.debug("User %s terminating the instance %s on %s" % (username, instance_iaas_id, cloud_name))

    timer = statsd.Timer('phantomweb')
    timer.start()

    timer_cloud = statsd.Timer('phantomweb')
    timer_cloud.start()

    try:
        iaas_connection.terminate_instances(instance_ids=[instance_iaas_id, ])
    except Exception:
        log.exception("Couldn't terminate %s" % instance_iaas_id)
    timer.stop('terminate_instances.timing')
    timer_cloud.stop('terminate_instances.%s.timing' % cloud_name)

    return
示例#2
0
def django_domain_load(request):
    user_obj = get_user_object(request.user.username)
    try:
        response_dict = phantom_domain_load(request.GET, user_obj)
        h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
    finally:
        user_obj.close()
    return h
示例#3
0
def django_instance_terminate(request):
    user_obj = get_user_object(request.user.username)
    try:
        response_dict = phantom_instance_terminate(request.POST, user_obj)
        h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
    finally:
        user_obj.close()
    return h
示例#4
0
def get_domain_instance(username, id, instance_id):
    user_obj = get_user_object(username)
    instances = user_obj.get_domain_instances(username, id)
    wanted_instance = None
    for instance in instances:
        if instance.get('id') == instance_id:
            wanted_instance = instance
            break
    return wanted_instance
示例#5
0
def get_domain_instance(username, id, instance_id):
    user_obj = get_user_object(username)
    instances = user_obj.get_domain_instances(username, id)
    wanted_instance = None
    for instance in instances:
        if instance.get('id') == instance_id:
            wanted_instance = instance
            break
    return wanted_instance
示例#6
0
def create_launch_configuration(username, name, cloud_params, context_params, appliance=None):
    lc = LaunchConfiguration.objects.create(name=name, username=username)

    user_obj = get_user_object(username)
    user_obj.create_dt(name, cloud_params, context_params, appliance)

    lc.save()

    return lc
示例#7
0
def get_all_domains(username):
    user_obj = get_user_object(username)
    domains = user_obj.get_all_domains(username)

    return_domains = []
    for d in domains:
        ent = user_obj.get_domain(username, d)
        return_domains.append(ent)

    return return_domains
示例#8
0
def get_all_domains(username):
    user_obj = get_user_object(username)
    domains = user_obj.get_all_domains(username)

    return_domains = []
    for d in domains:
        ent = user_obj.get_domain(username, d)
        return_domains.append(ent)

    return return_domains
示例#9
0
def update_launch_configuration(id, cloud_params, context_params, appliance=None):
    lc = get_launch_configuration(id)
    if lc is None:
        raise PhantomWebException("Trying to update lc %s that doesn't exist?" % id)

    username = lc.get('owner')
    name = lc.get('name')
    user_obj = get_user_object(username)
    user_obj.create_dt(name, cloud_params, context_params, appliance)

    return lc
示例#10
0
def get_launch_configuration(id):

    try:
        lc = LaunchConfiguration.objects.get(id=id)
    except LaunchConfiguration.DoesNotExist:
        return None

    lc_dict = {
        "id": lc.id,
        "name": lc.name,
        "owner": lc.username,
        "cloud_params": {}
    }

    user_obj = get_user_object(lc.username)
    dt = user_obj.get_dt(lc.name)
    if dt is None:
        log.error("DT %s doesn't seem to be in DTRS, continuing anyway" %
                  lc.name)
        dt = {}
    contextualization = dt.get('contextualization', {})

    if contextualization:
        userdata = contextualization.get("userdata")
        method = contextualization.get("method")
        run_list = contextualization.get("run_list")
        attributes = contextualization.get("attributes")
        if method == 'userdata' or userdata is not None:
            lc_dict["contextualization_method"] = 'user_data'
            lc_dict["user_data"] = userdata
        elif method == 'chef':
            lc_dict["contextualization_method"] = 'chef'
            lc_dict["chef_runlist"] = run_list
            lc_dict["chef_attributes"] = attributes
        elif method is None:
            lc_dict["contextualization_method"] = 'none'

    appliance = dt.get('appliance')
    if appliance:
        lc_dict['appliance'] = appliance

    for cloud, mapping in dt.get('mappings', {}).iteritems():

        lc_dict["cloud_params"][cloud] = {
            "max_vms": mapping.get('max_vms'),
            "common": mapping.get('common'),
            "rank": mapping.get('rank'),
            "image_id": mapping.get("iaas_image"),
            "instance_type": mapping.get("iaas_allocation")
        }

    return lc_dict
示例#11
0
def create_launch_configuration(username,
                                name,
                                cloud_params,
                                context_params,
                                appliance=None):
    lc = LaunchConfiguration.objects.create(name=name, username=username)

    user_obj = get_user_object(username)
    user_obj.create_dt(name, cloud_params, context_params, appliance)

    lc.save()

    return lc
示例#12
0
def remove_launch_configuration(username, lc_id):
    try:
        lc = LaunchConfiguration.objects.get(id=lc_id)
    except LaunchConfiguration.DoesNotExist:
        raise PhantomWebException("Could not delete launch configuration %s. Doesn't exist." % lc_id)

    user_obj = get_user_object(lc.username)
    try:
        user_obj.remove_dt(lc.name)
    except Exception:
        log.exception("Couldn't delete dt %s" % lc.name)

    lc.delete()
示例#13
0
def get_launch_configuration(id):

    try:
        lc = LaunchConfiguration.objects.get(id=id)
    except LaunchConfiguration.DoesNotExist:
        return None

    lc_dict = {
        "id": lc.id,
        "name": lc.name,
        "owner": lc.username,
        "cloud_params": {}
    }

    user_obj = get_user_object(lc.username)
    dt = user_obj.get_dt(lc.name)
    if dt is None:
        log.error("DT %s doesn't seem to be in DTRS, continuing anyway" % lc.name)
        dt = {}
    contextualization = dt.get('contextualization', {})

    if contextualization:
        userdata = contextualization.get("userdata")
        method = contextualization.get("method")
        run_list = contextualization.get("run_list")
        attributes = contextualization.get("attributes")
        if method == 'userdata' or userdata is not None:
            lc_dict["contextualization_method"] = 'user_data'
            lc_dict["user_data"] = userdata
        elif method == 'chef':
            lc_dict["contextualization_method"] = 'chef'
            lc_dict["chef_runlist"] = run_list
            lc_dict["chef_attributes"] = attributes
        elif method is None:
            lc_dict["contextualization_method"] = 'none'

    appliance = dt.get('appliance')
    if appliance:
        lc_dict['appliance'] = appliance

    for cloud, mapping in dt.get('mappings', {}).iteritems():

        lc_dict["cloud_params"][cloud] = {
            "max_vms": mapping.get('max_vms'),
            "common": mapping.get('common'),
            "rank": mapping.get('rank'),
            "image_id": mapping.get("iaas_image"),
            "instance_type": mapping.get("iaas_allocation")
        }

    return lc_dict
示例#14
0
def remove_launch_configuration(username, lc_id):
    try:
        lc = LaunchConfiguration.objects.get(id=lc_id)
    except LaunchConfiguration.DoesNotExist:
        raise PhantomWebException(
            "Could not delete launch configuration %s. Doesn't exist." % lc_id)

    user_obj = get_user_object(lc.username)
    try:
        user_obj.remove_dt(lc.name)
    except Exception:
        log.exception("Couldn't delete dt %s" % lc.name)

    lc.delete()
示例#15
0
def create_image_build(username, image_generator, additional_credentials={}):
    user_obj = get_user_object(username)
    all_clouds = user_obj.get_clouds()
    sites = {}
    credentials = {}
    for site in image_generator["cloud_params"]:
        try:
            cloud = all_clouds[site]
            sites[site] = cloud.site_desc
            credentials[site] = {
                "access_key": cloud.iaas_key,
                "secret_key": cloud.iaas_secret,
            }

            if sites[site]["type"] == "nimbus":
                try:
                    packer_credentials = PackerCredential.objects.get(username=username, cloud=site)
                    credentials[site]["canonical_id"] = packer_credentials.canonical_id
                    credentials[site]["usercert"] = packer_credentials.certificate
                    credentials[site]["userkey"] = packer_credentials.key
                except PackerCredential.DoesNotExist:
                    raise PhantomWebException("Could not find extra Nimbus credentials for image generation.")
            elif sites[site]["type"] == "openstack":
                try:
                    packer_credentials = PackerCredential.objects.get(username=username, cloud=site)
                    credentials[site]["openstack_username"] = packer_credentials.openstack_username
                    credentials[site]["openstack_password"] = packer_credentials.openstack_password
                    credentials[site]["openstack_project"] = packer_credentials.openstack_project
                except PackerCredential.DoesNotExist:
                    raise PhantomWebException("Could not find extra OpenStack credentials for image generation.")
                if site in additional_credentials:
                    openstack_password = additional_credentials[site].get("openstack_password")
                    if openstack_password is not None:
                        credentials[site]["openstack_password"] = openstack_password
        except KeyError:
            raise PhantomWebException("Could not get cloud %s" % site)

    result = packer_build.delay(image_generator, sites, credentials)

    image_build = ImageBuild.objects.create(
        image_generator_id=image_generator["id"],
        celery_task_id=result.id,
        status='submitted',
        returncode=-1,
        full_output="",
        cloud_name=site,
        owner=username)
    image_build.save()

    return {"id": image_build.id, "ready": result.ready(), "owner": username}
示例#16
0
def update_launch_configuration(id,
                                cloud_params,
                                context_params,
                                appliance=None):
    lc = get_launch_configuration(id)
    if lc is None:
        raise PhantomWebException(
            "Trying to update lc %s that doesn't exist?" % id)

    username = lc.get('owner')
    name = lc.get('name')
    user_obj = get_user_object(username)
    user_obj.create_dt(name, cloud_params, context_params, appliance)

    return lc
示例#17
0
def sites(request):
    user_obj = get_user_object(request.user.username)
    details = str_to_bool(request.GET.get('details', 'false'))
    all_sites = phantom_get_sites(request.GET, user_obj, details=details)
    response_list = []
    for site_name, site_dict in all_sites.iteritems():
        site_dict["credentials"] = "/api/%s/credentials/sites/%s" % (API_VERSION, site_name)
        site_dict["uri"] = "/api/%s/sites/%s" % (API_VERSION, site_name)
        if details:
            if site_dict.get('user_images') is None:
                site_dict['user_images'] = []
            if site_dict.get('public_images') is None:
                site_dict['public_images'] = []
        response_list.append(site_dict)
    h = HttpResponse(json.dumps(response_list), mimetype='application/javascript')
    return h
示例#18
0
def create_domain(username, name, parameters):
    user_obj = get_user_object(username)
    lc_name = parameters.get('lc_name')
    lc = get_launch_configuration_by_name(username, lc_name)
    if lc is None:
        raise PhantomWebException("No launch configuration named %s. Can't make domain" % lc_name)
    lc_dict = get_launch_configuration(lc.id)
    clouds = []
    for cloud_name, cloud in lc_dict.get('cloud_params', {}).iteritems():
        cloud = {
            'site_name': cloud_name,
            'rank': cloud.get('rank'),
            'size': cloud.get('max_vms'),
        }
        clouds.append(cloud)

    parameters['clouds'] = clouds
    return user_obj.add_domain(username, name, parameters)
示例#19
0
def create_domain(username, name, parameters):
    user_obj = get_user_object(username)
    lc_name = parameters.get('lc_name')
    lc = get_launch_configuration_by_name(username, lc_name)
    if lc is None:
        raise PhantomWebException(
            "No launch configuration named %s. Can't make domain" % lc_name)
    lc_dict = get_launch_configuration(lc.id)
    clouds = []
    for cloud_name, cloud in lc_dict.get('cloud_params', {}).iteritems():
        cloud = {
            'site_name': cloud_name,
            'rank': cloud.get('rank'),
            'size': cloud.get('max_vms'),
        }
        clouds.append(cloud)

    parameters['clouds'] = clouds
    return user_obj.add_domain(username, name, parameters)
示例#20
0
def site_resource(request, site):
    user_obj = get_user_object(request.user.username)
    details = str_to_bool(request.GET.get('details', 'false'))
    all_sites = phantom_get_sites(request.GET, user_obj, details=details)
    if site in all_sites:
        response_dict = {
            "id": site,
            "credentials": "/api/%s/credentials/sites/%s" % (API_VERSION, site),
            "instance_types": all_sites[site].get('instance_types', []),
            "uri": "/api/%s/sites/%s" % (API_VERSION, site)
        }
        if details:
            if response_dict.get('user_images') is None:
                response_dict['user_images'] = []
            if response_dict.get('public_images') is None:
                response_dict['public_images'] = []
        h = HttpResponse(json.dumps(response_dict), mimetype='application/javascript')
    else:
        h = HttpResponseNotFound('Site %s not found' % site, mimetype='application/javascript')
    return h
示例#21
0
def site_ssh_key_resource(request, site):
    user_obj = get_user_object(request.user.username)
    try:
        content = json.loads(request.body)
    except:
        msg = "Bad request (%s). No JSON. See API docs: %s" % (request.body, DOC_URI)
        return HttpResponseBadRequest(msg)

    required_params = ["name", "key"]
    if not has_all_required_params(required_params, content):
        return HttpResponseBadRequest("Bad request. Do not have all required parameters (%s)" % required_params)

    name = content['name']
    key = content['key']

    cloud = user_obj.get_cloud(site)
    upload_key(cloud, name, key)

    h = HttpResponse(request.body, status=201)
    return h
示例#22
0
def terminate_domain_instance(username, domain_id, instance_id):
    user_obj = get_user_object(username)
    instance_to_terminate = get_domain_instance(username, domain_id,
                                                instance_id)
    if instance_to_terminate is None:
        raise PhantomWebException("No instance %s available to terminate" %
                                  instance_id)

    instance_iaas_id = instance_to_terminate.get('iaas_instance_id')
    if instance_iaas_id is None:
        raise PhantomWebException("Instance %s has no iaas ID" % instance_id)

    cloud_name = instance_to_terminate.get('cloud')
    cloud_name = cloud_name.split("/")[-1]

    iaas_cloud = user_obj.get_cloud(cloud_name)
    iaas_connection = iaas_cloud.get_iaas_compute_con()

    log.debug("User %s terminating the instance %s on %s" %
              (username, instance_iaas_id, cloud_name))

    timer = statsd.Timer('phantomweb')
    timer.start()

    timer_cloud = statsd.Timer('phantomweb')
    timer_cloud.start()

    try:
        iaas_connection.terminate_instances(instance_ids=[
            instance_iaas_id,
        ])
    except Exception:
        log.exception("Couldn't terminate %s" % instance_iaas_id)
    timer.stop('terminate_instances.timing')
    timer_cloud.stop('terminate_instances.%s.timing' % cloud_name)

    return
示例#23
0
def create_image_build(username, image_generator, additional_credentials={}):
    user_obj = get_user_object(username)
    all_clouds = user_obj.get_clouds()
    sites = {}
    credentials = {}
    for site in image_generator["cloud_params"]:
        try:
            cloud = all_clouds[site]
            sites[site] = cloud.site_desc
            credentials[site] = {
                "access_key": cloud.iaas_key,
                "secret_key": cloud.iaas_secret,
            }

            if sites[site]["type"] == "nimbus":
                try:
                    packer_credentials = PackerCredential.objects.get(
                        username=username, cloud=site)
                    credentials[site][
                        "canonical_id"] = packer_credentials.canonical_id
                    credentials[site][
                        "usercert"] = packer_credentials.certificate
                    credentials[site]["userkey"] = packer_credentials.key
                except PackerCredential.DoesNotExist:
                    raise PhantomWebException(
                        "Could not find extra Nimbus credentials for image generation."
                    )
            elif sites[site]["type"] == "openstack":
                try:
                    packer_credentials = PackerCredential.objects.get(
                        username=username, cloud=site)
                    credentials[site][
                        "openstack_username"] = packer_credentials.openstack_username
                    credentials[site][
                        "openstack_password"] = packer_credentials.openstack_password
                    credentials[site][
                        "openstack_project"] = packer_credentials.openstack_project
                except PackerCredential.DoesNotExist:
                    raise PhantomWebException(
                        "Could not find extra OpenStack credentials for image generation."
                    )
                if site in additional_credentials:
                    openstack_password = additional_credentials[site].get(
                        "openstack_password")
                    if openstack_password is not None:
                        credentials[site][
                            "openstack_password"] = openstack_password
        except KeyError:
            raise PhantomWebException("Could not get cloud %s" % site)

    result = packer_build.delay(image_generator, sites, credentials)

    image_build = ImageBuild.objects.create(
        image_generator_id=image_generator["id"],
        celery_task_id=result.id,
        status='submitted',
        returncode=-1,
        full_output="",
        cloud_name=site,
        owner=username)
    image_build.save()

    return {"id": image_build.id, "ready": result.ready(), "owner": username}
示例#24
0
def modify_domain(username, id, parameters):
    user_obj = get_user_object(username)
    return user_obj.reconfigure_domain(username, id, parameters)
示例#25
0
def remove_domain(username, id):
    user_obj = get_user_object(username)
    return user_obj.remove_domain(username, id)
示例#26
0
def get_domain(username, id):
    user_obj = get_user_object(username)
    return user_obj.get_domain(username, id)
示例#27
0
def get_domain_instances(username, id):
    user_obj = get_user_object(username)
    return user_obj.get_domain_instances(username, id)
示例#28
0
def get_domain_instances(username, id):
    user_obj = get_user_object(username)
    return user_obj.get_domain_instances(username, id)
示例#29
0
def get_domain(username, id):
    user_obj = get_user_object(username)
    return user_obj.get_domain(username, id)
示例#30
0
def remove_domain(username, id):
    user_obj = get_user_object(username)
    return user_obj.remove_domain(username, id)
示例#31
0
def credentials_resource(request, site):
    user_obj = get_user_object(request.user.username)

    if request.method == "GET":
        all_clouds = user_obj.get_clouds()
        cloud = all_clouds.get(site)
        details = str_to_bool(request.GET.get('details', 'false'))
        if details is True:
            keys = get_all_keys([cloud])

        if cloud is not None:
            response_dict = {
                "id": cloud.cloudname,
                "access_key": cloud.iaas_key,
                "secret_key": cloud.iaas_secret,
                "key_name": cloud.keyname,
                "uri": "/api/%s/credentials/sites/%s" % (API_VERSION, cloud.cloudname)
            }
            if details is True:
                response_dict["available_keys"] = keys[cloud.cloudname]
            h = HttpResponse(json.dumps(response_dict), mimetype='application/javascript')
        else:
            h = HttpResponseNotFound('Credentials for site %s not found' % site, mimetype='application/javascript')
    elif request.method == "PUT":
        try:
            content = json.loads(request.body)
        except:
            return HttpResponseBadRequest()

        required_params = ["id", "access_key", "secret_key", "key_name"]
        if not has_all_required_params(required_params, content):
            return HttpResponseBadRequest()

        if site != content["id"]:
            return HttpResponseBadRequest()

        access_key = content["access_key"]
        secret_key = content["secret_key"]
        key_name = content["key_name"]

        # Check that the site exists
        all_sites = phantom_get_sites(request.REQUEST, user_obj)
        if site not in all_sites:
            return HttpResponseBadRequest()

        # Check that credentials exist
        if site not in user_obj.get_clouds():
            return HttpResponseBadRequest()

        response_dict = {
            "id": site,
            "access_key": access_key,
            "secret_key": secret_key,
            "key_name": key_name,
            "uri": "/api/%s/credentials/sites/%s" % (API_VERSION, site)
        }

        # Add credentials to DTRS
        try:
            user_obj.add_site(site, access_key, secret_key, key_name)
        except:
            log.exception("Failed to add credentials for site %s" % site)
            return HttpResponseServerError()

        h = HttpResponse(json.dumps(response_dict), mimetype='application/javascript')
    elif request.method == "DELETE":
        # Check that credentials exist
        clouds = user_obj.get_clouds()
        if site not in clouds:
            return HttpResponseBadRequest("Site %s not available. Choose from %s" % (site, clouds.keys()))

        # Remove credentials from DTRS
        try:
            user_obj.delete_site(site)
        except:
            msg = "Failed to remove credentials for site %s" % site
            log.exception(msg)
            return HttpResponseServerError(msg)

        h = HttpResponse(status=204)

    return h
示例#32
0
def credentials(request):
    user_obj = get_user_object(request.user.username)

    if request.method == "GET":
        all_clouds = user_obj.get_clouds()
        details = str_to_bool(request.GET.get('details', 'false'))
        if details is True:
            keys = get_all_keys(all_clouds)
            packer_credentials = get_all_packer_credentials(request.user.username, all_clouds)

        response_list = []
        for cloud in all_clouds.values():
            credentials_name = cloud.cloudname
            credentials_dict = {
                "id": credentials_name,
                "access_key": cloud.iaas_key,
                "secret_key": cloud.iaas_secret,
                "key_name": cloud.keyname,
                "uri": "/api/%s/credentials/sites/%s" % (API_VERSION, credentials_name)
            }
            if details is True:
                credentials_dict["available_keys"] = keys[cloud.cloudname]
                packer_cloud_creds = packer_credentials[cloud.cloudname]
                if "usercert" in packer_cloud_creds:
                    credentials_dict["nimbus_user_cert"] = packer_cloud_creds["usercert"]
                if "userkey" in packer_cloud_creds:
                    credentials_dict["nimbus_user_key"] = packer_cloud_creds["userkey"]
                if "canonical_id" in packer_cloud_creds:
                    credentials_dict["nimbus_canonical_id"] = packer_cloud_creds["canonical_id"]
                if "openstack_username" in packer_cloud_creds:
                    credentials_dict["openstack_username"] = packer_cloud_creds["openstack_username"]
                if "openstack_password" in packer_cloud_creds:
                    credentials_dict["openstack_password"] = packer_cloud_creds["openstack_password"]
                if "openstack_project" in packer_cloud_creds:
                    credentials_dict["openstack_project"] = packer_cloud_creds["openstack_project"]
            response_list.append(credentials_dict)
        log.info(response_list)
        h = HttpResponse(json.dumps(response_list), mimetype='application/javascript')
    elif request.method == "POST":
        try:
            content = json.loads(request.body)
        except:
            msg = "Bad request (%s). No JSON. See API docs: %s" % (request.body, DOC_URI)
            return HttpResponseBadRequest(msg)

        required_params = ["id", "access_key", "secret_key", "key_name"]
        if not has_all_required_params(required_params, content):
            return HttpResponseBadRequest("Bad request. Do not have all required parameters (%s)" % required_params)

        site = content["id"]
        access_key = content["access_key"]
        secret_key = content["secret_key"]
        key_name = content["key_name"]
        nimbus_user_cert = content.get("nimbus_user_cert")
        nimbus_user_key = content.get("nimbus_user_key")
        nimbus_canonical_id = content.get("nimbus_canonical_id")
        openstack_username = content.get("openstack_username")
        openstack_password = content.get("openstack_password")
        openstack_project = content.get("openstack_project")

        # Check that the site exists
        all_sites = phantom_get_sites(request.POST, user_obj)
        if site not in all_sites:
            return HttpResponseBadRequest("%s doesn't seem to exist. I know about %s" % (
                site, all_sites))

        if re.search("^%s+$" % ACCEPTED_RESOURCE_PATTERN, site) is None:
            return HttpResponseBadRequest("%s isn't an acceptable id. Must match %s" % (
                site, ACCEPTED_RESOURCE_PATTERN))

        response_dict = {
            "id": site,
            "access_key": access_key,
            "secret_key": secret_key,
            "key_name": key_name,
            "uri": "/api/%s/credentials/sites/%s" % (API_VERSION, site)
        }

        # Add credentials to DTRS
        try:
            user_obj.add_site(site, access_key, secret_key, key_name)
        except:
            log.exception("Failed to add credentials for site %s" % site)
            return HttpResponseServerError()

        # Add image generation credentials to DB
        if nimbus_user_cert is not None:
            add_packer_credentials(username=request.user.username, cloud=site, nimbus_user_cert=nimbus_user_cert,
                    nimbus_user_key=nimbus_user_key, nimbus_canonical_id=nimbus_canonical_id)

        if openstack_username is not None:
            add_packer_credentials(username=request.user.username, cloud=site, openstack_username=openstack_username,
                    openstack_password=openstack_password, openstack_project=openstack_project)

        response_dict["nimbus_user_cert"] = nimbus_user_cert
        response_dict["nimbus_user_key"] = nimbus_user_key
        response_dict["nimbus_canonical_id"] = nimbus_canonical_id
        response_dict["openstack_username"] = openstack_username
        response_dict["openstack_password"] = openstack_password
        response_dict["openstack_project"] = openstack_project

        h = HttpResponse(json.dumps(response_dict), status=201, mimetype='application/javascript')

    return h
示例#33
0
def chef_credentials(request):
    user_obj = get_user_object(request.user.username)

    if request.method == "GET":
        all_credentials = user_obj.get_chef_credentials()

        response_list = []
        for credential_name, credential in all_credentials.iteritems():
            credentials_dict = {
                "id": credential_name,
                "server_url": credential['url'],
                "client_name": credential.get('client_name', 'admin'),
                "validation_client_name": credential.get('validation_client_name', 'chef-validator'),
                "client_key": credential['client_key'],
                "validator_key": credential['validator_key'],
                "uri": "/api/%s/credentials/chef/%s" % (API_VERSION, credential_name)
            }
            response_list.append(credentials_dict)
        h = HttpResponse(json.dumps(response_list), mimetype='application/javascript')
    elif request.method == "POST":
        try:
            content = json.loads(request.body)
        except:
            msg = "Bad request (%s). No JSON. See API docs: %s" % (request.body, DOC_URI)
            return HttpResponseBadRequest(msg)

        required_params = ["id", "server_url", "client_name", "client_key", "validator_key"]
        if not has_all_required_params(required_params, content):
            return HttpResponseBadRequest("Bad request. Do not have all required parameters (%s)" % required_params)

        name = content["id"]
        url = content["server_url"]
        client_name = content["client_name"]
        validation_client_name = content.get("validation_client_name", "chef-validator")
        client_key = content["client_key"]
        validator_key = content["validator_key"]

        if re.search("^%s+$" % ACCEPTED_RESOURCE_PATTERN, name) is None:
            return HttpResponseBadRequest("%s isn't an acceptable id. Must match %s" % (
                name, ACCEPTED_RESOURCE_PATTERN))

        # Check that the site exists
        all_credentials = user_obj.get_chef_credentials()
        if name in all_credentials:
            return HttpResponseRedirect("/api/%s/credentials/chef/%s" % (API_VERSION, name))

        response_dict = {
            "id": name,
            "server_url": url,
            "validation_client_name": validation_client_name,
            "client_name": client_name,
            "client_key": client_key,
            "validator_key": validator_key,
            "uri": "/api/%s/credentials/chef/%s" % (API_VERSION, name)
        }

        # Add credentials to DTRS
        try:
            user_obj.add_chef_credentials(name, url, client_name, client_key, validator_key,
                    validation_client_name=validation_client_name)
        except:
            msg = "Failed to add credentials for %s" % name
            raise
            log.exception(msg)
            return HttpResponseServerError(msg)

        h = HttpResponse(json.dumps(response_dict), status=201, mimetype='application/javascript')

    return h
示例#34
0
def chef_credentials_resource(request, site):
    user_obj = get_user_object(request.user.username)

    if request.method == "GET":
        all_credentials = user_obj.get_chef_credentials()
        credential = all_credentials.get(site)

        if credential is not None:
            response_dict = {
                "id": site,
                "server_url": credential['url'],
                "client_name": credential['client_name'],
                "client_key": credential['client_key'],
                "validator_key": credential['validator_key'],
                "validation_client_name": credential.get('validation_client_name', 'chef-validator'),
                "uri": "/api/%s/credentials/chef/%s" % (API_VERSION, site)
            }
            h = HttpResponse(json.dumps(response_dict), mimetype='application/javascript')
        else:
            h = HttpResponseNotFound('Credentials for site %s not found' % site, mimetype='application/javascript')
        return h
    elif request.method == "PUT":
        try:
            content = json.loads(request.body)
        except:
            msg = "Bad request (%s). No JSON. See API docs: %s" % (request.body, DOC_URI)
            return HttpResponseBadRequest(msg)

        required_params = ["server_url", "client_key", "client_name", "validator_key"]
        if not has_all_required_params(required_params, content):
            return HttpResponseBadRequest("Bad request. Do not have all required parameters (%s)" % required_params)

        name = site
        url = content["server_url"]
        client_name = content["client_name"]
        validation_client_name = content.get("validation_client_name", 'chef-validator')
        client_key = content["client_key"]
        validator_key = content["validator_key"]

        # Check that the credentials exist
        all_credentials = user_obj.get_chef_credentials()
        if site not in all_credentials:
            return HttpResponseBadRequest()

        response_dict = {
            "id": name,
            "server_url": url,
            "client_name": client_name,
            "client_key": client_key,
            "validator_key": validator_key,
            "validation_client_name": validation_client_name,
            "uri": "/api/%s/credentials/chef/%s" % (API_VERSION, name)
        }

        # Add credentials to DTRS
        try:
            user_obj.add_chef_credentials(name, url, client_name, client_key, validator_key,
                    validation_client_name=validation_client_name)
        except:
            log.exception("Failed to add credentials for site %s" % site)
            return HttpResponseServerError()

        h = HttpResponse(json.dumps(response_dict), mimetype='application/javascript')

        return h

    elif request.method == "DELETE":
        # Check that the credentials exist
        all_credentials = user_obj.get_chef_credentials()
        if site not in all_credentials:
            return HttpResponseBadRequest()

        # Remove credentials from DTRS
        try:
            user_obj.delete_chef_credentials(site)
        except:
            msg = "Failed to remove credentials for site %s" % site
            log.exception(msg)
            return HttpResponseServerError(msg)

        h = HttpResponse(status=204)

        return h
示例#35
0
def modify_domain(username, id, parameters):
    user_obj = get_user_object(username)
    return user_obj.reconfigure_domain(username, id, parameters)