File: storage_account.rb

package info (click to toggle)
ruby-gitlab-fog-azure-rm 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 756 kB
  • sloc: ruby: 5,926; sh: 9; makefile: 4
file content (92 lines) | stat: -rw-r--r-- 3,233 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
92
module Fog
  module AzureRM
    class Storage
      # This class is giving implementation of create/save and
      # delete/destroy for storage account.
      class StorageAccount < Fog::Model
        identity  :name
        attribute :id
        attribute :location
        attribute :resource_group
        attribute :sku_name
        attribute :replication
        attribute :encryption
        attribute :tags

        def self.parse(storage_account)
          hash = {}
          hash['id'] = storage_account.id
          hash['name'] = storage_account.name
          hash['location'] = storage_account.location
          hash['resource_group'] = get_resource_group_from_id(storage_account.id)
          hash['sku_name'] = storage_account.sku.name.split('_').first
          hash['replication'] = storage_account.sku.name.split('_').last
          hash['encryption'] = storage_account.encryption.services.blob.enabled unless storage_account.encryption.nil?
          hash['tags'] = storage_account.tags
          hash
        end

        def save
          requires :name
          requires :location
          requires :resource_group
          # Create a model for new storage account.
          self.sku_name = STANDARD_STORAGE if sku_name.nil?
          self.replication = ALLOWED_STANDARD_REPLICATION.first if replication.nil?
          validate_sku_name!
          storage_account = service.create_storage_account(storage_account_params)
          merge_attributes(Fog::AzureRM::Storage::StorageAccount.parse(storage_account))
        end

        def storage_account_params
          {
            resource_group: resource_group,
            name: name,
            sku_name: sku_name,
            location: location,
            replication: replication,
            encryption: encryption,
            tags: tags
          }
        end

        def update(storage_account_params)
          validate_input(storage_account_params)
          storage_account_params = merge_attributes(storage_account_params).all_attributes

          storage_account = service.update_storage_account(storage_account_params)
          merge_attributes(Fog::AzureRM::Storage::StorageAccount.parse(storage_account))
        end

        def validate_sku_name!
          case sku_name
          when STANDARD_STORAGE
            raise 'Standard Replications can only be LRS, ZRS, GRS or RAGRS.' unless ALLOWED_STANDARD_REPLICATION.include?(replication)
          when PREMIUM_STORAGE
            raise 'Premium Replication can only be LRS.' if replication != 'LRS'
          else
            raise 'Account Type can only be Standard or Premium'
          end
        end

        def get_access_keys(options = {})
          service.get_storage_access_keys(resource_group, name, options)
        end

        def destroy
          service.delete_storage_account(resource_group, name)
        end

        private

        def validate_input(attr_hash)
          invalid_attr = [:resource_group, :name, :location, :id]
          result = invalid_attr & attr_hash.keys
          raise 'Cannot modify the given attribute' unless result.empty?
        end

        private :storage_account_params, :validate_sku_name!
      end
    end
  end
end