示例#1
0
    def __init__(self,
                 base_url=None,
                 auth_url=None,
                 api_url=None,
                 stream_url=None,
                 api_version=None,
                 cacert=None,
                 debug=False,
                 token=None,
                 api_key=None):
        # Get CLI options. If not given, then try to get it from the environment.
        self.endpoints = dict()

        # Populate the endpoints
        if base_url:
            self.endpoints['base'] = base_url
        else:
            self.endpoints['base'] = os.environ.get('ST2_BASE_URL',
                                                    DEFAULT_BASE_URL)

        api_version = api_version or os.environ.get('ST2_API_VERSION',
                                                    DEFAULT_API_VERSION)

        self.endpoints['exp'] = '%s:%s/%s' % (self.endpoints['base'],
                                              DEFAULT_API_PORT, 'exp')

        if api_url:
            self.endpoints['api'] = api_url
        else:
            self.endpoints['api'] = os.environ.get(
                'ST2_API_URL', '%s:%s/%s' %
                (self.endpoints['base'], DEFAULT_API_PORT, api_version))

        if auth_url:
            self.endpoints['auth'] = auth_url
        else:
            self.endpoints['auth'] = os.environ.get(
                'ST2_AUTH_URL',
                '%s:%s' % (self.endpoints['base'], DEFAULT_AUTH_PORT))

        if stream_url:
            self.endpoints['stream'] = stream_url
        else:
            self.endpoints['stream'] = os.environ.get(
                'ST2_STREAM_URL', '%s:%s/%s' %
                (self.endpoints['base'], DEFAULT_STREAM_PORT, api_version))

        if cacert is not None:
            self.cacert = cacert
        else:
            self.cacert = os.environ.get('ST2_CACERT', None)

        # Note: boolean is also a valid value for "cacert"
        is_cacert_string = isinstance(self.cacert, six.string_types)
        if (self.cacert and is_cacert_string
                and not os.path.isfile(self.cacert)):
            raise ValueError('CA cert file "%s" does not exist.' %
                             (self.cacert))

        self.debug = debug

        # Note: This is a nasty hack for now, but we need to get rid of the decrator abuse
        if token:
            os.environ['ST2_AUTH_TOKEN'] = token

        self.token = token

        if api_key:
            os.environ['ST2_API_KEY'] = api_key

        self.api_key = api_key

        # Instantiate resource managers and assign appropriate API endpoint.
        self.managers = dict()
        self.managers['Token'] = ResourceManager(models.Token,
                                                 self.endpoints['auth'],
                                                 cacert=self.cacert,
                                                 debug=self.debug)
        self.managers['RunnerType'] = ResourceManager(models.RunnerType,
                                                      self.endpoints['api'],
                                                      cacert=self.cacert,
                                                      debug=self.debug)
        self.managers['Action'] = ResourceManager(models.Action,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['ActionAlias'] = ActionAliasResourceManager(
            models.ActionAlias,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['ActionAliasExecution'] = ActionAliasExecutionManager(
            models.ActionAliasExecution,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['ApiKey'] = ResourceManager(models.ApiKey,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['Config'] = ConfigManager(models.Config,
                                                self.endpoints['api'],
                                                cacert=self.cacert,
                                                debug=self.debug)
        self.managers['ConfigSchema'] = ResourceManager(models.ConfigSchema,
                                                        self.endpoints['api'],
                                                        cacert=self.cacert,
                                                        debug=self.debug)
        self.managers['LiveAction'] = LiveActionResourceManager(
            models.LiveAction,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['Inquiry'] = InquiryResourceManager(
            models.Inquiry,
            self.endpoints['exp'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['Pack'] = PackResourceManager(models.Pack,
                                                    self.endpoints['api'],
                                                    cacert=self.cacert,
                                                    debug=self.debug)
        self.managers['Policy'] = ResourceManager(models.Policy,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['PolicyType'] = ResourceManager(models.PolicyType,
                                                      self.endpoints['api'],
                                                      cacert=self.cacert,
                                                      debug=self.debug)
        self.managers['Rule'] = ResourceManager(models.Rule,
                                                self.endpoints['api'],
                                                cacert=self.cacert,
                                                debug=self.debug)
        self.managers['Sensor'] = ResourceManager(models.Sensor,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['TriggerType'] = ResourceManager(models.TriggerType,
                                                       self.endpoints['api'],
                                                       cacert=self.cacert,
                                                       debug=self.debug)
        self.managers['Trigger'] = ResourceManager(models.Trigger,
                                                   self.endpoints['api'],
                                                   cacert=self.cacert,
                                                   debug=self.debug)
        self.managers['TriggerInstance'] = TriggerInstanceResourceManager(
            models.TriggerInstance,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['KeyValuePair'] = ResourceManager(models.KeyValuePair,
                                                        self.endpoints['api'],
                                                        cacert=self.cacert,
                                                        debug=self.debug)
        self.managers['Webhook'] = ResourceManager(models.Webhook,
                                                   self.endpoints['api'],
                                                   cacert=self.cacert,
                                                   debug=self.debug)
        self.managers['Timer'] = ResourceManager(models.Timer,
                                                 self.endpoints['api'],
                                                 cacert=self.cacert,
                                                 debug=self.debug)
        self.managers['Trace'] = ResourceManager(models.Trace,
                                                 self.endpoints['api'],
                                                 cacert=self.cacert,
                                                 debug=self.debug)
        self.managers['RuleEnforcement'] = ResourceManager(
            models.RuleEnforcement,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['Stream'] = StreamManager(self.endpoints['stream'],
                                                cacert=self.cacert,
                                                debug=self.debug)

        # RBAC
        self.managers['Role'] = ResourceManager(models.Role,
                                                self.endpoints['api'],
                                                cacert=self.cacert,
                                                debug=self.debug)
        self.managers['UserRoleAssignment'] = ResourceManager(
            models.UserRoleAssignment,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
示例#2
0
文件: client.py 项目: Itxaka/st2
    def __init__(self,
                 base_url=None,
                 auth_url=None,
                 api_url=None,
                 api_version=None,
                 cacert=None,
                 debug=False,
                 token=None):
        # Get CLI options. If not given, then try to get it from the environment.
        self.endpoints = dict()

        # Populate the endpoints
        if base_url:
            self.endpoints['base'] = base_url
        else:
            self.endpoints['base'] = os.environ.get('ST2_BASE_URL',
                                                    DEFAULT_BASE_URL)

        api_version = api_version or os.environ.get('ST2_API_VERSION',
                                                    DEFAULT_API_VERSION)

        if api_url:
            self.endpoints['api'] = api_url
        else:
            self.endpoints['api'] = os.environ.get(
                'ST2_API_URL', '%s:%s/%s' %
                (self.endpoints['base'], DEFAULT_API_PORT, api_version))

        if auth_url:
            self.endpoints['auth'] = auth_url
        else:
            self.endpoints['auth'] = os.environ.get(
                'ST2_AUTH_URL',
                '%s:%s' % (self.endpoints['base'], DEFAULT_AUTH_PORT))

        if cacert:
            self.cacert = cacert
        else:
            self.cacert = os.environ.get('ST2_CACERT', None)

        if self.cacert and not os.path.isfile(self.cacert):
            raise ValueError('CA cert file "%s" does not exist.' %
                             (self.cacert))

        self.debug = debug

        # Note: This is a nasty hack for now, but we need to get rid of the decrator abuse
        if token:
            os.environ['ST2_AUTH_TOKEN'] = token

        self.token = token

        # Instantiate resource managers and assign appropriate API endpoint.
        self.managers = dict()
        self.managers['Token'] = ResourceManager(models.Token,
                                                 self.endpoints['auth'],
                                                 cacert=self.cacert,
                                                 debug=self.debug)
        self.managers['RunnerType'] = ResourceManager(models.RunnerType,
                                                      self.endpoints['api'],
                                                      cacert=self.cacert,
                                                      debug=self.debug)
        self.managers['Action'] = ResourceManager(models.Action,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['ActionAlias'] = ActionAliasResourceManager(
            models.ActionAlias,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['LiveAction'] = LiveActionResourceManager(
            models.LiveAction,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['Policy'] = ResourceManager(models.Policy,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['PolicyType'] = ResourceManager(models.PolicyType,
                                                      self.endpoints['api'],
                                                      cacert=self.cacert,
                                                      debug=self.debug)
        self.managers['Rule'] = ResourceManager(models.Rule,
                                                self.endpoints['api'],
                                                cacert=self.cacert,
                                                debug=self.debug)
        self.managers['Sensor'] = ResourceManager(models.Sensor,
                                                  self.endpoints['api'],
                                                  cacert=self.cacert,
                                                  debug=self.debug)
        self.managers['TriggerType'] = ResourceManager(models.TriggerType,
                                                       self.endpoints['api'],
                                                       cacert=self.cacert,
                                                       debug=self.debug)
        self.managers['Trigger'] = ResourceManager(models.Trigger,
                                                   self.endpoints['api'],
                                                   cacert=self.cacert,
                                                   debug=self.debug)
        self.managers['TriggerInstance'] = TriggerInstanceResourceManager(
            models.TriggerInstance,
            self.endpoints['api'],
            cacert=self.cacert,
            debug=self.debug)
        self.managers['KeyValuePair'] = ResourceManager(models.KeyValuePair,
                                                        self.endpoints['api'],
                                                        cacert=self.cacert,
                                                        debug=self.debug)
        self.managers['Webhook'] = ResourceManager(models.Webhook,
                                                   self.endpoints['api'],
                                                   cacert=self.cacert,
                                                   debug=self.debug)
        self.managers['Trace'] = ResourceManager(models.Trace,
                                                 self.endpoints['api'],
                                                 cacert=self.cacert,
                                                 debug=self.debug)