Пример #1
0
 def do_help(self, args):
     """
     Display help about this program or one of its subcommands.
     """
     if args.command:
         if args.command in self.subcommands:
             self.subcommands[args.command].print_help()
         else:
             raise exc.CommandError("'%s' is not a valid subcommand" %
                                    args.command)
     else:
         self.parser.print_help()
Пример #2
0
def find_resource(manager, name_or_id):
    """Helper for the _find_* methods."""
    # first try to get entity as integer id
    if isinstance(name_or_id, int) or name_or_id.isdigit():
        return manager.get(int(name_or_id))

    if sys.version_info <= (3, 0):
        name_or_id = strutils.safe_decode(name_or_id)

    # now try to get entity as uuid
    try:
        uuid.UUID(name_or_id)
        return manager.get(name_or_id)
    except (ValueError, exceptions.NotFound):
        pass

    try:
        try:
            return manager.find(human_id=name_or_id)
        except exceptions.NotFound:
            pass

        # finally try to find entity by name
        try:
            return manager.find(name=name_or_id)
        except exceptions.NotFound:
            try:
                return manager.find(display_name=name_or_id)
            except (UnicodeDecodeError, exceptions.NotFound):
                try:
                    return manager.find(display_name=name_or_id)
                except exceptions.NotFound:
                    msg = "No %s with a name or ID of '%s' exists." % \
                          (manager.resource_class.__name__.lower(), name_or_id)
                    raise exceptions.CommandError(msg)
    except exceptions.NoUniqueMatch:
        msg = ("Multiple %s matches found for '%s', use an ID to be more"
               " specific." %
               (manager.resource_class.__name__.lower(), name_or_id))
        raise exceptions.CommandError(msg)
Пример #3
0
    def property_update(self, zone, property_key, property_value):

        zone_dict = zone._info
        props_dict = zone_dict['properties']
        if property_key in props_dict:
            props_dict[property_key] = property_value
        else:
            msg = "No zone property with a key: '%s' exists." % property_key
            raise exceptions.CommandError(msg)

        zone_dict['properties'] = props_dict
        del zone_dict['_links']
        zone_body = {"zone": zone_dict}

        return self._update("/zones/%s" % base.getid(zone), zone_body)
Пример #4
0
    def delete(self, property_key):
        """
        Delete the property key, property value

        :param property_key: the key of the property
        :param property_value: the value the tue property
        """
        properties = self.list()
        if property_key in properties:
            del properties[property_key]
            return self._update("/properties", properties)
        else:
            msg = "No %s with a key '%s' exists." % \
                  (self.resource_class.__name__.lower(), property_key)
            raise exceptions.CommandError(msg)
Пример #5
0
    def create(self, property_key, property_value):
        """
        Create a property.

        :param property_key: the key of the property
        :param property_value: the value the tue property
        """
        properties = self.list()
        if property_key not in properties:
            properties[property_key] = property_value
        else:
            msg = "A %s with a key: '%s' exists." % \
                  (self.resource_class.__name__.lower(), property_key)
            raise exceptions.CommandError(msg)

        return self._update("/properties", properties)
Пример #6
0
    def property_delete(self, architecture, profile, property_key):

        profile_dict = profile._info
        props_dict = profile_dict['properties']

        if property_key in props_dict:
            del props_dict[property_key]
        else:
            msg = "No profile property with a key: '%s' exists." % \
                  (property_key)
            raise exceptions.CommandError(msg)

        profile_dict['properties'] = props_dict
        del profile_dict['_links']
        profile_body = {"profile": profile_dict}

        return self._update(
            "/archs/%s/profiles/%s" %
            (base.getid(architecture), base.getid(profile)), profile_body)
Пример #7
0
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
            options.os_automation_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        subcommand_parser = self.get_subcommand_parser(
            options.os_automation_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        (os_username, os_password, os_tenant_name, os_auth_url,
         os_region_name, os_tenant_id, endpoint_type, insecure,
         service_type, service_name, username, apikey, projectid,
         url, region_name, cacert) = \
            (args.os_username, args.os_password,
             args.os_tenant_name, args.os_auth_url,
             args.os_region_name, args.os_tenant_id,
             args.endpoint_type, args.insecure,
             args.service_type, args.service_name,
             args.username, args.apikey, args.projectid,
             args.url, args.region_name, args.os_cacert)

        if not endpoint_type:
            endpoint_type = DEFAULT_AUTOMATION_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_AUTOMATION_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        #FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.

        if not utils.isunauthenticated(args.func):
            if not os_username:
                if not username:
                    raise exc.CommandError(
                        "You must provide a username "
                        "via either --os-username or env[OS_USERNAME]")
                else:
                    os_username = username

            if not os_password:
                if not apikey:
                    raise exc.CommandError("You must provide a password "
                                           "via either --os-password or via "
                                           "env[OS_PASSWORD]")
                else:
                    os_password = apikey

            if not (os_tenant_name or os_tenant_id):
                if not projectid:
                    raise exc.CommandError("You must provide a tenant_id "
                                           "via either --os-tenant-id or "
                                           "env[OS_TENANT_ID]")
                else:
                    os_tenant_name = projectid

            if not os_auth_url:
                if not url:
                    raise exc.CommandError(
                        "You must provide an auth url "
                        "via either --os-auth-url or env[OS_AUTH_URL]")
                else:
                    os_auth_url = url

            if not os_region_name and region_name:
                os_region_name = region_name

        if not (os_tenant_name or os_tenant_id):
            raise exc.CommandError(
                "You must provide a tenant_id "
                "via either --os-tenant-id or env[OS_TENANT_ID]")

        if not os_auth_url:
            raise exc.CommandError(
                "You must provide an auth url "
                "via either --os-auth-url or env[OS_AUTH_URL]")

        self.cs = client.Client(options.os_automation_api_version,
                                os_username,
                                os_password,
                                os_tenant_name,
                                os_auth_url,
                                insecure,
                                region_name=os_region_name,
                                tenant_id=os_tenant_id,
                                endpoint_type=endpoint_type,
                                extensions=self.extensions,
                                service_type=service_type,
                                service_name=service_name,
                                retries=options.retries,
                                http_log_debug=args.debug,
                                cacert=cacert)

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("Invalid OpenStack Automation credentials.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user")

        endpoint_api_version = \
            self.cs.get_automation_api_version_from_endpoint()
        if endpoint_api_version != options.os_automation_api_version:
            msg = (("Automation API version is set to %s "
                    "but you are accessing a %s endpoint. "
                    "Change its value via either --os-automation-api-version "
                    "or env[OS_AUTOMATION_API_VERSION]") %
                   (options.os_automation_api_version, endpoint_api_version))
            raise exc.InvalidAPIVersion(msg)

        args.func(self.cs, args)