def delete(self, uri, logon_required=True): """ Perform the HTTP DELETE method against the resource identified by a URI, on the faked HMC. Parameters: uri (:term:`string`): Relative URI path of the resource, e.g. "/api/session/{session-id}". This URI is relative to the base URL of the session (see the :attr:`~zhmcclient.Session.base_url` property). Must not be `None`. logon_required (bool): Boolean indicating whether the operation requires that the session is logged on to the HMC. For example, for the logoff operation, it does not make sense to first log on. Because this is a faked HMC, this does not perform a real logon, but it is still used to update the state in the faked HMC. Raises: :exc:`~zhmcclient.HTTPError` :exc:`~zhmcclient.ParseError` (not implemented) :exc:`~zhmcclient.AuthError` (not implemented) :exc:`~zhmcclient.ConnectionError` """ try: self._urihandler.delete(self._hmc, uri, logon_required) except HTTPError as exc: raise zhmcclient.HTTPError(exc.response()) except ConnectionError as exc: raise zhmcclient.ConnectionError(exc.message, None)
def create_check_mode_pwrule(console, create_props, update_props): """ Create and return a fake local Password Rule object. This is used when a password rule needs to be created in check mode. This function must be consistent with the behavior of the "Create Password Rule" operation on the HMC. HTTP errors the HMC would return are indicated by raising zhmcclient.HTTPError. """ input_props = {} input_props.update(create_props) input_props.update(update_props) # Check required input properties missing_props = [] for pname in ('name', ): if pname not in input_props: missing_props.append(pname) if missing_props: raise zhmcclient.HTTPError({ 'http-status': 400, 'reason': 4, 'message': "Required input properties missing for Create Password Rule: {p}". format(p=missing_props), }) # Defaults for properties props = { # createable/updateable 'description': '', 'expiration': 0, 'min-length': 8, 'max-length': 256, 'consecutive-characters': 0, 'similarity-count': 0, 'history-count': 0, 'case-sensitive': False, 'character-rules': [], # read-only 'type': 'user-defined', 'replication-overwrite-possible': True, } # Apply specified input properties on top of the defaults props.update(input_props) pwrule_oid = 'fake-{0}'.format(uuid.uuid4()) pwrule = console.password_rules.resource_object(pwrule_oid, props=props) return pwrule
def create_check_mode_urole(client, create_props, update_props): """ Create and return a fake local User Role object. This is used when a user role needs to be created in check mode. This function must be consistent with the behavior of the "Create User Role" operation on the HMC. HTTP errors the HMC would return are indicated by raising zhmcclient.HTTPError. """ console = client.consoles.console input_props = {} input_props.update(create_props) input_props.update(update_props) # Check required input properties missing_props = [] for pname in ('name',): if pname not in input_props: missing_props.append(pname) if missing_props: raise zhmcclient.HTTPError({ 'http-status': 400, 'reason': 4, 'message': "Required input properties missing for " "Create User Role: {p}".format(p=missing_props), }) optasks_urole = console.user_roles.find(name='hmc-operator-tasks') # Defaults for properties props = { # createable/updateable 'description': '', 'associated-system-defined-user-role-uri': optasks_urole.uri, 'is-inheritance-enabled': False, 'permissions': [], # read-only 'type': 'user-defined', 'replication-overwrite-possible': True, } # Apply specified input properties on top of the defaults props.update(input_props) urole_oid = 'fake-{0}'.format(uuid.uuid4()) urole = console.user_roles.resource_object(urole_oid, props=props) return urole
def post(self, uri, body=None, logon_required=True, wait_for_completion=True, operation_timeout=None): """ Perform the HTTP POST method against the resource identified by a URI, using a provided request body, on the faked HMC. HMC operations using HTTP POST are either synchronous or asynchronous. Asynchronous operations return the URI of an asynchronously executing job that can be queried for status and result. Examples for synchronous operations: * With no response body: "Logon", "Update CPC Properties" * With a response body: "Create Partition" Examples for asynchronous operations: * With no ``job-results`` field in the completed job status response: "Start Partition" * With a ``job-results`` field in the completed job status response (under certain conditions): "Activate a Blade", or "Set CPC Power Save" The `wait_for_completion` parameter of this method can be used to deal with asynchronous HMC operations in a synchronous way. Parameters: uri (:term:`string`): Relative URI path of the resource, e.g. "/api/session". This URI is relative to the base URL of the session (see the :attr:`~zhmcclient.Session.base_url` property). Must not be `None`. body (:term:`json object`): JSON object to be used as the HTTP request body (payload). `None` means the same as an empty dictionary, namely that no HTTP body is included in the request. logon_required (bool): Boolean indicating whether the operation requires that the session is logged on to the HMC. For example, the "Logon" operation does not require that. Because this is a faked HMC, this does not perform a real logon, but it is still used to update the state in the faked HMC. wait_for_completion (bool): Boolean controlling whether this method should wait for completion of the requested HMC operation, as follows: * If `True`, this method will wait for completion of the requested operation, regardless of whether the operation is synchronous or asynchronous. This will cause an additional entry in the time statistics to be created for the asynchronous operation and waiting for its completion. This entry will have a URI that is the targeted URI, appended with "+completion". * If `False`, this method will immediately return the result of the HTTP POST method, regardless of whether the operation is synchronous or asynchronous. operation_timeout (:term:`number`): Timeout in seconds, when waiting for completion of an asynchronous operation. The special value 0 means that no timeout is set. `None` means that the default async operation timeout of the session is used. For `wait_for_completion=True`, a :exc:`~zhmcclient.OperationTimeout` is raised when the timeout expires. For `wait_for_completion=False`, this parameter has no effect. Returns: :term:`json object`: If `wait_for_completion` is `True`, returns a JSON object representing the response body of the synchronous operation, or the response body of the completed job that performed the asynchronous operation. If a synchronous operation has no response body, `None` is returned. If `wait_for_completion` is `False`, returns a JSON object representing the response body of the synchronous or asynchronous operation. In case of an asynchronous operation, the JSON object will have a member named ``job-uri``, whose value can be used with the :meth:`~zhmcclient.Session.query_job_status` method to determine the status of the job and the result of the original operation, once the job has completed. See the section in the :term:`HMC API` book about the specific HMC operation and about the 'Query Job Status' operation, for a description of the members of the returned JSON objects. Raises: :exc:`~zhmcclient.HTTPError` :exc:`~zhmcclient.ParseError` (not implemented) :exc:`~zhmcclient.AuthError` (not implemented) :exc:`~zhmcclient.ConnectionError` """ try: return self._urihandler.post(self._hmc, uri, body, logon_required, wait_for_completion) except HTTPError as exc: raise zhmcclient.HTTPError(exc.response()) except ConnectionError as exc: raise zhmcclient.ConnectionError(exc.message, None)
def create_check_mode_user(console, create_props, update_props): """ Create and return a fake local User object. This is used when a user needs to be created in check mode. This function must be consistent with the behavior of the "Create User" operation on the HMC. HTTP errors the HMC would return are indicated by raising zhmcclient.HTTPError. """ input_props = {} input_props.update(create_props) input_props.update(update_props) # Check required input properties missing_props = [] for pname in ('name', 'type', 'authentication-type'): if pname not in input_props: missing_props.append(pname) name = input_props['name'] user_type = input_props['type'] auth_type = input_props['authentication-type'] if auth_type == 'local': for pname in ('password-rule-uri', 'password'): if pname not in input_props: missing_props.append(pname) if auth_type == 'ldap': for pname in ('ldap-server-definition-uri'): if pname not in input_props: missing_props.append(pname) mfa_types = input_props.get('mfa-types', []) if 'mfa-server' in mfa_types: for pname in ('primary-mfa-server-definition-uri', 'mfa-policy'): if pname not in input_props: missing_props.append(pname) if missing_props: raise zhmcclient.HTTPError({ 'http-status': 400, 'reason': 4, 'message': "Required input properties missing for Create User: {p}". format(p=missing_props), }) # Defaults for optional properties that are the same in all cases props = { 'description': '', 'session-timeout': 0, 'verify-timeout': 15, 'idle-timeout': 0, 'max-failed-logins': 3, 'disable-delay': 1, 'inactivity-timeout': 0, 'disruptive-pw-required': True, 'disruptive-text-required': False, 'allow-remote-access': False, 'allow-management-interfaces': False, 'max-web-services-api-sessions': 100, 'web-services-api-session-idle-timeout': 360, 'user-roles': [], 'default-group-uri': None, 'replication-overwrite-possible': False, # Default not in WS-API book 'multi-factor-authentication-required': False, 'email-address': None, 'mfa-types': None, } # Defaults for optional properties that depend on the case if user_type == 'pattern-based': props['user-pattern-uri'] = None if user_type == 'template': props['user-template-uri'] = None if user_type != 'template': props['disabled'] = False if auth_type == 'local': props['password-rule-uri'] = None props['password'] = None props['password-expires'] = None props['force-password-change'] = True props['min-pw-change-time'] = 0 if auth_type == 'ldap': props['ldap-server-definition-uri'] = None if user_type != 'template': props['userid-on-ldap-server'] = '' mfa_required = input_props.get( 'multi-factor-authentication-required', False) if mfa_required: props['force-shared-secret-key-change'] = False if 'mfa-server' in mfa_types: props['primary-mfa-server-definition-uri'] = None props['backup-mfa-server-definition-uri'] = None props['mfa-policy'] = None if user_type != 'template': props['mfa-userid'] = name if user_type == 'template': props['mfa-userid-override'] = None # Apply specified input properties on top of the defaults props.update(input_props) user_oid = 'fake-{0}'.format(uuid.uuid4()) user = console.users.resource_object(user_oid, props=props) return user