def create_swift_password(identity_client, user_id, description, module): result = {} try: cspd = CreateSwiftPasswordDetails() cspd.description = description # create_response = oci_utils.call_with_backoff(identity_client.create_swift_password, user_id=user_id, # create_swift_password_details=cspd) # sw_pass_id = create_response.data.id result = oci_utils.create_resource( resource_type=RESOURCE_NAME, create_fn=identity_client.create_swift_password, kwargs_create={ "user_id": user_id, "create_swift_password_details": cspd }, module=module) resource = result[RESOURCE_NAME] sw_pass_id = resource['id'] get_logger().info("Created Swift Password %s", to_dict(resource)) response = identity_client.list_swift_passwords(user_id) # wait until the created Swift password reaches Active state oci.wait_until(identity_client, response, evaluate_response=lambda resp: _is_swift_password_active(resp.data, sw_pass_id)) result[RESOURCE_NAME] = to_dict( _get_swift_password_from_id(identity_client, user_id, sw_pass_id, module)) return result except ServiceError as ex: module.fail_json(msg=ex.message) except MaximumWaitTimeExceeded as mwte: module.fail_json(msg=str(mwte))
def create_swift_password(identity_client, user_id, description, module): result = {} try: cspd = CreateSwiftPasswordDetails() cspd.description = description result = oci_utils.create_resource( resource_type=RESOURCE_NAME, create_fn=identity_client.create_swift_password, kwargs_create={ "user_id": user_id, "create_swift_password_details": cspd }, module=module, ) resource = result[RESOURCE_NAME] sw_pass_id = resource["id"] # cache the swift password's password as it is only provided during creation cached_pass = resource["password"] get_logger().info("Created Swift Password %s", to_dict(resource)) response = identity_client.list_swift_passwords(user_id) # wait until the created Swift password reaches Active state oci.wait_until( identity_client, response, evaluate_response=lambda resp: _is_swift_password_active( resp.data, sw_pass_id), ) sw_pass = _get_swift_password_from_id(identity_client, user_id, sw_pass_id, module) # stuff the cached password in the returned swift_password model sw_pass.password = cached_pass result[RESOURCE_NAME] = to_dict(sw_pass) return result except ServiceError as ex: module.fail_json(msg=ex.message) except MaximumWaitTimeExceeded as mwte: module.fail_json(msg=str(mwte))
def main(): set_logger(oci_utils.get_logger("oci_swift_password")) module_args = oci_utils.get_common_arg_spec(supports_create=True) module_args.update( dict(user_id=dict(type='str', required=True), swift_password_id=dict(type='str', required=False, aliases=['id'], no_log=True), description=dict(type='str', required=False), state=dict(type='str', required=False, default='present', choices=['present', 'absent']))) module = AnsibleModule( argument_spec=module_args, supports_check_mode=False, required_if=[('state', 'absent', ['swift_password_id'])], ) if not HAS_OCI_PY_SDK: module.fail_json(msg='oci python sdk required for this module.') identity_client = oci_utils.create_service_client(module, IdentityClient) state = module.params['state'] result = dict(changed=False) user_id = module.params.get("user_id", None) id = module.params.get("swift_password_id", None) description = module.params.get('description', None) get_logger().debug("Id is " + str(id)) if id is not None: sw_pass = _get_swift_password_from_id(identity_client, user_id, id, module) if state == 'absent': get_logger().debug( "Delete swift password %s for user %s requested", id, user_id) if sw_pass: get_logger().debug("Deleting %s", sw_pass.id) result = delete_swift_password(identity_client, user_id, id, module) else: get_logger().debug("Swift Password %s already deleted.", id) elif state == 'present': if sw_pass.description != description: result = update_swift_password(identity_client, user_id, id, description, module) else: # No change needed, return existing swift password details result[RESOURCE_NAME] = to_dict(sw_pass) else: # Check and create swift password if necessary result = oci_utils.check_and_create_resource( resource_type=RESOURCE_NAME, create_fn=create_swift_password, kwargs_create={ "identity_client": identity_client, "user_id": user_id, "description": description, "module": module }, list_fn=identity_client.list_swift_passwords, kwargs_list={"user_id": user_id}, module=module, model=CreateSwiftPasswordDetails()) module.exit_json(**result)