Huawei OceanStor API Automation

If you are looking to automate some tasks in your Huawei OceanStor instead of doing manual work, the API provided by Huawei for the software can be a simple solution for your needs that requires no setup or configuration. In this case, we will be using ansible to automate the process and the calls, so we will need that package installed. The used ansible core version in this example is the 2.14.
The OceanStor API documentation can be quite extensive, so we will let here just a couple of examples of usage, that hopefully will help you understand and maybe save some time. For any other needs check out the official documentation and feel free to build on top of this template.

"Work smarter, not harder." Allen F. Morgenstern

This ansible playbook can be ran from your local machine or from a remote server, depending on your environment, as long as the ansible packages are installed and present. The playbook as is, will run locally, connect to the Huawei OceanStor API on the default port 8088 and authenticate the user. In this example, we will backup a volume using the snapshot and clone method, first getting all the available volumes, after creating the clone and the snapshot for the selected one.

NOTE: It's possible to skip the authentication by removing the first task and getting the values from the login variable to the headers in the later tasks.

---
- hosts: localhost # Host with network flow to the Huawei OceanStor API
  gather_facts: no 
  vars:
    time: "{{ ansible_date_time.epoch_int }}"
    source_lun: lun_name # LUN to operate on
    ip: 10.0.0.10 # Huawei OceanStor API address
    port: 8088 # Huawei OceanStor API port
    username: admin
    password: ''
    scope: 0 # Scope 0 is for local users, 1 is for LDAP
  tasks:
    - name: API Login
      uri:
        url: "https://{{ ip }}:{{ port }}/deviceManager/rest/xxxxx/sessions"
        method: POST
        body_format: json
        body: 
          username: "{{ username }}"
          password: "{{ password }}"
          scope: "{{ scope }}"
        timeout: 10
        validate_certs: no
      register: login # Will return the variables for headers later use for the authenticated user

    - debug:
        var: login

    - name: Get LUNs
      uri:
        url: "https://{{ ip }}:{{ port }}/deviceManager/rest/{{ login.json.data.deviceid }}/lun"
        method: GET
        timeout: 5
        validate_certs: no
        headers:
         Content-Type: "{{ login.content_type }}"
         iBaseToken: "{{ login.json.data.iBaseToken }}"
         Cookie: "{{ login.set_cookie }}"
      register: unmap # returns a list of LUNs

    - debug:
        var: unmap.json.data

    - name: Clone LUN {{ source_lun }}
      uri:
        url: "https://{{ ip }}:{{ port }}/deviceManager/rest/{{ login.json.data.deviceid }}/lun"
        method: POST
        body_format: json
        body: 
          isclone: true
          name: "dstore_{{ time }}_{{ mysql_instance }}"
          clonesourceid: '{{ unmap.somevalue }}'
        timeout: 5
        validate_certs: no
        headers:
         Content-Type: "{{ login.content_type }}"
         iBaseToken: "{{ login.json.data.iBaseToken }}"
         Cookie: "{{ login.set_cookie }}"
      register: cloned

    - name: Mapping LUN {{ cloned.json.instanceId }}
      uri:
        url: "https://{{ ip }}:{{ port }}/api/v2/mapping"
        method: POST
        body_format: json
        body: '{ "lunId": "", "hostName": "{{ Server }}" }' # Can be used lunName instead
        timeout: 5
        validate_certs: no
        headers:
         Content-Type: "{{ login.content_type }}"
         iBaseToken: "{{ login.json.data.iBaseToken }}"
         Cookie: "{{ login.set_cookie }}"
      register: mapped
      when:  cloned.json | length >= 1

To run the playbook simply execute the following line in the terminal:

ansible-playbook /path/to/created_playbook.yml

Disclaimer: This playbook should be used as an example only and should not be used directly in production environments, since there can be quite the differences between software configurations that can result in misconfigurations or worse incidents like data loss. Consult with a specialist and test the play before in some sort of development environment. Reach out if any support or development is needed.