def sources_add(socket_path, api_version, args, show_json=False): """Add or change a source :param socket_path: Path to the Unix socket to use for API communication :type socket_path: str :param api_version: Version of the API to talk to. eg. "0" :type api_version: str :param args: List of remaining arguments from the cmdline :type args: list of str :param show_json: Set to True to show the JSON output instead of the human readable output :type show_json: bool sources add <source.toml> """ api_route = client.api_url(api_version, "/projects/source/new") rval = 0 for source in argify(args): if not os.path.exists(source): log.error("Missing source file: %s", source) continue source_toml = open(source, "r").read() result = client.post_url_toml(socket_path, api_route, source_toml) if handle_api_result(result, show_json): rval = 1 return rval
def blueprints_workspace(socket_path, api_version, args, show_json=False): """Push the blueprint TOML to the temporary workspace storage :param socket_path: Path to the Unix socket to use for API communication :type socket_path: str :param api_version: Version of the API to talk to. eg. "0" :type api_version: str :param args: List of remaining arguments from the cmdline :type args: list of str :param show_json: Set to True to show the JSON output instead of the human readable output :type show_json: bool blueprints workspace <blueprint> Push the blueprint TOML to the temporary workspace storage. """ api_route = client.api_url(api_version, "/blueprints/workspace") rval = 0 for blueprint in argify(args): if not os.path.exists(blueprint): log.error("Missing blueprint file: %s", blueprint) continue blueprint_toml = open(blueprint, "r").read() result = client.post_url_toml(socket_path, api_route, blueprint_toml) if show_json: print(json.dumps(result, indent=4)) for err in result.get("errors", []): log.error(err) # Any errors results in returning a 1, but we continue with the rest first if not result.get("status", False): rval = 1 return rval
def blueprints_workspace(socket_path, api_version, args, show_json=False): """Push the blueprint TOML to the temporary workspace storage :param socket_path: Path to the Unix socket to use for API communication :type socket_path: str :param api_version: Version of the API to talk to. eg. "0" :type api_version: str :param args: List of remaining arguments from the cmdline :type args: list of str :param show_json: Set to True to show the JSON output instead of the human readable output :type show_json: bool blueprints workspace <blueprint> Push the blueprint TOML to the temporary workspace storage. """ api_route = client.api_url(api_version, "/blueprints/workspace") rval = 0 for blueprint in argify(args): if not os.path.exists(blueprint): log.error("Missing blueprint file: %s", blueprint) continue with open(blueprint, "r") as f: blueprint_toml = f.read() result = client.post_url_toml(socket_path, api_route, blueprint_toml) if handle_api_result(result, show_json)[0]: rval = 1 return rval
def blueprints_push(socket_path, api_version, args, show_json=False): """Push a blueprint TOML file to the server, updating the blueprint :param socket_path: Path to the Unix socket to use for API communication :type socket_path: str :param api_version: Version of the API to talk to. eg. "0" :type api_version: str :param args: List of remaining arguments from the cmdline :type args: list of str :param show_json: Set to True to show the JSON output instead of the human readable output :type show_json: bool push <blueprint> Push a blueprint TOML file to the server. """ api_route = client.api_url(api_version, "/blueprints/new") rval = 0 for blueprint in argify(args): if not os.path.exists(blueprint): log.error("Missing blueprint file: %s", blueprint) continue blueprint_toml = open(blueprint, "r").read() result = client.post_url_toml(socket_path, api_route, blueprint_toml) if handle_api_result(result, show_json): rval = 1 return rval
def run_module(): """Module main function """ # define available arguments/parameters a user can pass to the module module_args = dict( name=dict(type='str', required=False), definition=dict(type='str', required=False), state=dict(type='str', required=False, default='present'), ) mutually_exclusive = [('name', 'definition')] # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ansible_module_results='') # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=module_args, mutually_exclusive=mutually_exclusive, supports_check_mode=True, ) # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications if module.check_mode: module.exit_json(**result) # Figure out the name of the blueprint, either from the name or the TOML definition if module.params['name']: blueprint_name = module.params['name'] else: blueprint_name = toml.loads(module.params['definition'])['name'] result['blueprint_name'] = blueprint_name # See what is already present blueprint_present = True try: available_blueprint = client.get_url_raw( SOCKET, '/api/v1/blueprints/info/' + blueprint_name + '?format=toml') except RuntimeError: # The composer.http_client throws a Runtime Error if the API returns 400 blueprint_present = False if module.params['state'] == 'present' and not blueprint_present: result['ansible_module_results'] = client.post_url_toml( SOCKET, '/api/v1/blueprints/new', module.params['definition']) result['ansible_module_results']['definition'] = client.get_url_raw( SOCKET, '/api/v1/blueprints/info/' + blueprint_name + '?format=toml') result['changed'] = True elif module.params['state'] == 'present' \ and blueprint_present \ and available_blueprint != module.params['definition']: result['ansible_module_results'] = client.post_url_toml( SOCKET, '/api/v1/blueprints/new', module.params['definition']) result['ansible_module_results']['definition'] = client.get_url_raw( SOCKET, '/api/v1/blueprints/info/' + blueprint_name + '?format=toml') result['changed'] = True elif module.params['state'] == 'absent' and blueprint_present: result['ansible_module_results'] = client.delete_url_json( SOCKET, '/api/v1/blueprints/delete/' + blueprint_name) result['changed'] = True else: result['ansible_module_results']['definition'] = client.get_url_raw( SOCKET, '/api/v1/blueprints/info/' + blueprint_name + '?format=toml') result['changed'] = False # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results module.exit_json(**result)