 - name: Create storage account name
   set_fact:
       storage_account: "{{ resource_group | hash('md5') | truncate(24, True, '') }}"

 - name: Test invalid account name
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}" 
       name: "invalid_char$"
   register: invalid_name     
   ignore_errors: yes  

 - name: Assert task failed
   assert: { that: "invalid_name['failed'] == True" }

 - name: Delete storage account
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}" 
       name: "{{ storage_account }}"
       state: absent
       force_delete_nonempty: True

 - name: Create new storage account
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}" 
       name: "{{ storage_account }}"
       account_type: Standard_LRS
       append_tags: no
       blob_cors:
          - allowed_origins:
              - http://www.example.com/
            allowed_methods:
              - GET
              - POST
            allowed_headers:
              - x-ms-meta-data*
              - x-ms-meta-target*
              - x-ms-meta-abc
            exposed_headers:
              - x-ms-meta-*
            max_age_in_seconds: 200
       tags:
           test: test
           galaxy: galaxy
   register: output

 - name: Assert status succeeded and results include an Id value 
   assert:
     that:
       - output.changed
       - output.state.id is defined
       - output.state.blob_cors | length == 1

 - name: Create new storage account (idempotence)
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}" 
       name: "{{ storage_account }}"
       account_type: Standard_LRS
       append_tags: no
       blob_cors:
          - allowed_origins:
              - http://www.example.com/
            allowed_methods:
              - GET
              - POST
            allowed_headers:
              - x-ms-meta-data*
              - x-ms-meta-target*
              - x-ms-meta-abc
            exposed_headers:
              - x-ms-meta-*
            max_age_in_seconds: 200
       tags:
           test: test
           galaxy: galaxy
   register: output
  
 - assert:
     that:
        - not output.changed

 - name: Gather facts by tags
   azure_rm_storageaccount_facts:
       resource_group: "{{ resource_group }}"
       tags:
         - test
         - galaxy

 - assert:
       that: azure_storageaccounts | length >= 1

 - name: Change account type 
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}" 
       name: "{{ storage_account }}"
       account_type: Premium_LRS
   register: change_account
   ignore_errors: yes 

 - name: Assert account type change failed 
   assert: { that: "change_account['failed'] == True" }

 - name: Change account type and add custom domain
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}"
       name: "{{ storage_account }}"
       account_type: Standard_GRS
       custom_domain: { name: ansible.com, use_sub_domain: no }
   register: change_account
   ignore_errors: yes

 - name: Assert CNAME failure
   assert: { that: "'Azure Error: StorageCustomDomainNameNotValid' in  change_account['msg']" }

 - name: Update account tags
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}"
       name: "{{ storage_account }}"
       append_tags: no
       tags:
           testing: testing
           delete: never
   register: output

 - assert:
       that:
           - "output.state.tags | length == 2"
           - "output.state.tags.testing == 'testing'"
           - "output.state.tags.delete == 'never'"

 - name: Gather facts
   azure_rm_storageaccount_facts:
       resource_group: "{{ resource_group }}"
       name: "{{ storage_account }}"
       show_connection_string: True
       show_blob_cors: True

 - assert:
       that:
           - "azure_storageaccounts| length == 1"
           - "storageaccounts | length == 1"
           - not storageaccounts[0].custom_domain
           - storageaccounts[0].account_type == "Standard_GRS"
           - storageaccounts[0].primary_endpoints.blob.connectionstring
           - storageaccounts[0].blob_cors

 - name: Gather facts
   azure_rm_storageaccount_facts:
       resource_group: "{{ resource_group }}"

 - assert:
       that:
           - "azure_storageaccounts | length > 0"

 - name: Delete acccount
   azure_rm_storageaccount:
       resource_group: "{{ resource_group }}" 
       name: "{{ storage_account }}"
       state: absent
