def run(self, args, **kwargs): resources = resource.load_meta_file(args.file) if not resources: print('No %s found in %s.' % (self.resource.get_display_name().lower(), args.file)) return None if not isinstance(resources, list): resources = [resources] instances = [] for res in resources: # pick only the meaningful properties. data = { 'user': res['user'], # required 'key_hash': res['key_hash'], # required 'metadata': res.get('metadata', {}), 'enabled': res.get('enabled', False) } if 'id' in res: data['id'] = res['id'] instance = self.resource.deserialize(data) try: result = self.manager.update(instance, **kwargs) except requests.exceptions.HTTPError as e: if e.response.status_code == http_client.NOT_FOUND: instance = self.resource.deserialize(data) # Key doesn't exist yet, create it instead result = self.manager.create(instance, **kwargs) else: raise e instances.append(result) return instances
def run(self, args, **kwargs): # normalize the file path to allow for relative files to be specified file_path = os.path.normpath(pjoin(os.getcwd(), args.file)) # load the data (JSON/YAML) from the file kvps = resource.load_meta_file(file_path) instances = [] # bail out if file was empty if not kvps: return instances # if the data is not a list (ie. it's a single entry) # then make it a list so our process loop is generic if not isinstance(kvps, list): kvps = [kvps] for item in kvps: # parse required KeyValuePair properties name = item['name'] value = item['value'] # parse optional KeyValuePair properties scope = item.get('scope', DEFAULT_CUD_SCOPE) user = item.get('user', None) secret = item.get('secret', False) ttl = item.get('ttl', None) # if the value is not a string, convert it to JSON # all keys in the datastore must strings if not isinstance(value, six.string_types): if args.convert: value = json.dumps(value) else: raise ValueError( ("Item '%s' has a value that is not a string." " Either pass in the -c/--convert option to convert" " non-string types to JSON strings automatically, or" " convert the data to a string in the file") % name) # create the KeyValuePair instance instance = KeyValuePair() instance.id = name # TODO: refactor and get rid of id instance.name = name instance.value = value instance.scope = scope if user: instance.user = user if secret: instance.secret = secret if ttl: instance.ttl = ttl # call the API to create/update the KeyValuePair self.manager.update(instance, **kwargs) instances.append(instance) return instances
def run(self, args, **kwargs): # normalize the file path to allow for relative files to be specified file_path = os.path.normpath(pjoin(os.getcwd(), args.file)) # load the data (JSON/YAML) from the file kvps = resource.load_meta_file(file_path) # if the data is not a list (ie. it's a single entry) # then make it a list so our process loop is generic if not isinstance(kvps, list): kvps = [kvps] instances = [] for item in kvps: # parse required KeyValuePair properties name = item['name'] value = item['value'] # parse optional KeyValuePair properties scope = item.get('scope', DEFAULT_SCOPE) user = item.get('user', None) secret = item.get('secret', False) ttl = item.get('ttl', None) # if the value is not a string, convert it to JSON # all keys in the datastore must strings if not isinstance(value, six.string_types): if args.convert: value = json.dumps(value) else: raise ValueError(("Item '%s' has a value that is not a string." " Either pass in the -c/--convert option to convert" " non-string types to JSON strings automatically, or" " convert the data to a string in the file") % name) # create the KeyValuePair instance instance = KeyValuePair() instance.id = name # TODO: refactor and get rid of id instance.name = name instance.value = value instance.scope = scope if user: instance.user = user if secret: instance.secret = secret if ttl: instance.ttl = ttl # call the API to create/update the KeyValuePair self.manager.update(instance, **kwargs) instances.append(instance) return instances
def run(self, args, **kwargs): resources = resource.load_meta_file(args.file) if not resources: print('No %s found in %s.' % (self.resource.get_display_name().lower(), args.file)) return None if not isinstance(resources, list): resources = [resources] instances = [] for res in resources: # pick only the meaningful properties. instance = { 'user': res['user'], # required 'key_hash': res['key_hash'], # required 'metadata': res.get('metadata', {}), 'enabled': res.get('enabled', False) } instance = self.resource.deserialize(instance) instances.append(self.manager.create(instance, **kwargs)) return instances