File: directory.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 (153 lines) | stat: -rw-r--r-- 5,833 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module Fog
  module AzureRM
    class Storage
      # This class is giving implementation of create and delete a container.
      class Directory < Fog::Model
        VALID_ACLS = ['container', 'blob', 'unknown', nil].freeze

        include Fog::AzureRM::Utilities::General

        attr_writer :acl

        identity  :key,                 aliases: %w(Name name)
        attribute :etag,                aliases: %w(Etag ETag)
        attribute :last_modified,       aliases: %w(Last-Modified LastModified), type: 'time'
        attribute :lease_duration,      aliases: %w(Lease-Duration LeaseDuration)
        attribute :lease_state,         aliases: %w(Lease-State LeaseState)
        attribute :lease_status,        aliases: %w(Lease-Status LeaseStatus)

        attribute :metadata

        # Set the the permission
        #
        # @param new_acl [String] permission. Options: container, blob or nil. unknown is for internal usage.
        #                                     container: Full public read access. Container and blob data can be read via anonymous request.
        #                                                Clients can enumerate blobs within the container via anonymous request, but cannot
        #                                                enumerate containers within the storage account.
        #                                     blob: Public read access for blobs only. Blob data within this container can be read via
        #                                           anonymous request, but container data is not available. Clients cannot enumerate blobs
        #                                           within the container via anonymous request.
        #                                     nil: No public read access. Container and blob data can be read by the account owner only.
        #                                     unknown: Internal usage in fog-azure-rm.
        #
        # @return [String] Permission.
        #
        # @exception ArgumentError Raised when new_acl is not 'container', 'blob', nil or 'unknown'.
        #
        # Reference: https://msdn.microsoft.com/en-us/library/azure/dd179391.aspx
        #
        def acl=(new_acl)
          raise ArgumentError.new("acl must be one of [#{VALID_ACLS.join(', ')}nil]") unless VALID_ACLS.include?(new_acl)

          attributes[:acl] = new_acl
        end

        # Get the the permission
        #
        #     required attributes: key
        #
        # @return [String] Permission.
        #
        def acl
          requires :key

          return attributes[:acl] if attributes[:acl] != 'unknown' || !persisted?

          data = service.get_container_acl(key)
          attributes[:acl] = data[0]
        end

        # Destroy directory.
        #
        #     required attributes: key
        #
        # @return [Boolean] True if successful
        #
        def destroy
          requires :key

          service.delete_container(key)
        end

        # Get files under this directory.
        # If you have set max_results or max_keys when getting this directory by directories.get,
        # files may be incomplete. You need to use files.all to get all files under this directory.
        #
        # @return [Fog::AzureRM::Storage::Files] Files.
        #
        def files
          @files ||= Fog::AzureRM::Storage::Files.new(directory: self, service: service)
        end

        # Set the container permission to public or private
        #
        # @param [Boolean] True: public(container); False: private(nil)
        #
        # @return [Boolean] True if public; Otherwise return false.
        #
        def public=(new_public)
          attributes[:acl] = new_public ? 'container' : nil
          new_public
        end

        # Get the public URL of the directory
        #
        #     required attributes: key
        #
        # @param options [Hash]
        # @option options [String] scheme Sets which URL to get, http or https. Options: https or http. Default is https.
        #
        # @return [String] A public URL.
        #
        def public_url(options = {})
          requires :key

          service.get_container_url(key, options) if acl == 'container'
        end

        # Create/Update the directory
        #
        #     required attributes: key
        #
        # @param options [Hash]
        # @option options [Boolean] is_create Sets whether to create or update the directory. Default is true(create).
        #                                     Will update metadata and acl when is_create is set to false.
        #
        # @return [Boolean] True if successful.
        #
        def save(options = {})
          requires :key

          is_create = options.delete(:is_create)
          if is_create.nil? || is_create
            options = {}
            options[:public_access_level] = acl if acl != 'unknown'
            options[:metadata] = metadata if metadata

            container = service.create_container(key, options)
          else
            service.put_container_acl(key, acl) if acl != 'unknown'
            service.put_container_metadata(key, metadata) if metadata
            container = service.get_container_properties(key)
          end

          attributes[:is_persisted] = true
          data = parse_storage_object(container)
          merge_attributes(data)

          true
        end

        # Check whether the directory is created.
        #
        # @return [Boolean] True if the file is created. Otherwise return false.
        #
        def persisted?
          # is_persisted is true in case of directories.get or after #save
          # last_modified is set in case of directories.all
          attributes[:is_persisted] || !attributes[:last_modified].nil?
        end
      end
    end
  end
end