Class: TerraspacePluginAzurerm::Interfaces::Summary

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Terraspace::Plugin::Summary::Interface
Defined in:
lib/terraspace_plugin_azurerm/interfaces/summary.rb

Instance Method Summary collapse

Instance Method Details

#bucket_fieldObject

interface method



9
10
11
# File 'lib/terraspace_plugin_azurerm/interfaces/summary.rb', line 9

def bucket_field
  'storage_account_name'
end

#container_nameObject



49
50
51
# File 'lib/terraspace_plugin_azurerm/interfaces/summary.rb', line 49

def container_name
  @info['container_name']
end

#delete_blob(key) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/terraspace_plugin_azurerm/interfaces/summary.rb', line 34

def delete_blob(key)
  blob_client.delete_blob(container_name, key)
rescue Azure::Core::Http::HTTPError => e
  case e.message
  when /BlobNotFound/
    logger.info "WARN: #{e.class}: #{e.message}"
    logger.info "Blob item does not exist: #{key}"
  when /ContainerNotFound/
    logger.info "WARN: #{e.class}: #{e.message}"
    logger.info "Container not found: #{container_name}"
  else
    raise
  end
end

#delete_empty_statefile(key) ⇒ Object

interface method



30
31
32
# File 'lib/terraspace_plugin_azurerm/interfaces/summary.rb', line 30

def delete_empty_statefile(key)
  delete_blob(key)
end

#downloadObject

interface method



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/terraspace_plugin_azurerm/interfaces/summary.rb', line 14

def download
  marker = nil
  loop do
    blobs = list_blobs(container_name, marker: marker)
    blobs.each do |blob|
      blob, content = blob_client.get_blob(container_name, blob.name)
      local_path = "#{@dest}/#{blob.name}"
      FileUtils.mkdir_p(File.dirname(local_path))
      File.open(local_path, 'wb') {|f| f.write(content)}
    end
    marker = blobs.continuation_token
    break unless marker && !marker.empty?
  end
end

#list_blobs(container_name, marker:) ⇒ Object

Friendly error handling for user



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/terraspace_plugin_azurerm/interfaces/summary.rb', line 54

def list_blobs(container_name, marker:)
  blob_client.list_blobs(container_name, marker: marker, prefix: @folder)
rescue Azure::Core::Http::HTTPError => e
  if e.message.include?("AuthenticationFailed")
    logger.error "e.class #{e.class}: #{e.message}"
    logger.error "Unable to authenticate to download the statefiles from the storage account.".color(:red)
    if ENV['AZURE_STORAGE_ACCESS_KEY']
      logger.error <<~EOL
        It looks like you have the AZURE_STORAGE_ACCESS_KEY environment variable set.
        It may be the incorrect key for the storage account: #{@info['storage_account_name']}
        Try unsetting it:

            unset AZURE_STORAGE_ACCESS_KEY

        When the environment variable is not set, this library will try to fetch the key for you.
        Or you can try setting the correct key.
      EOL
    else
      logger.error <<~EOL
        The fetched storage access key did not seem to authenticate successfully.
        Try setting the AZURE_STORAGE_ACCESS_KEY environment variable with access to the storage account: #{@info['storage_account_name']}
      EOL
    end
    # Common message
    logger.error <<~EOL
      One way to get the key is with:

          az storage account keys list --account-name #{@info['storage_account_name']} --resource-group #{@info['resource_group_name']}

      Then you can set it:

          export AZURE_STORAGE_ACCESS_KEY=[replace-with-value]
    EOL
    exit 1
  else
    raise
  end
end