Class: Fog::Storage::HP::Directory

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/hp/models/storage/directory.rb

Instance Attribute Summary

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Instance Method Details

#acl=(new_acl) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/hp/models/storage/directory.rb', line 15

def acl=(new_acl)
  if new_acl.nil?
    new_acl = "private"
  end
  valid_acls = ['private', 'public-read', 'public-write', 'public-read-write']
  unless valid_acls.include?(new_acl)
    raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]")
  end
  @acl = new_acl
end

#cdn_enable=(new_cdn_enable) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/fog/hp/models/storage/directory.rb', line 80

def cdn_enable=(new_cdn_enable)
  @cdn_enable ||= false
  if (!connection.cdn.nil? && connection.cdn.enabled?)
    @cdn_enable = new_cdn_enable
  else
    # since cdn service is not activated, container cannot be cdn-enabled
    @cdn_enable = false
  end
end

#cdn_enabled?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fog/hp/models/storage/directory.rb', line 90

def cdn_enabled?
  if (!connection.cdn.nil? && connection.cdn.enabled?)
    begin response = connection.cdn.head_container(key)
      cdn_header = response.headers.fetch('X-Cdn-Enabled', nil)
      if (!cdn_header.nil? && cdn_header == 'True')
        @cdn_enable = true
      else
        @cdn_enable = false
      end
    rescue Fog::CDN::HP::NotFound => err
      @cdn_enable = false
    end
  else
    @cdn_enable = false
  end
end

#cdn_public_urlObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fog/hp/models/storage/directory.rb', line 107

def cdn_public_url
  requires :key
  @cdn_public_url ||= begin
    # return the CDN public url from the appropriate uri from the header
    begin response = connection.cdn.head_container(key)
      if response.headers['X-Cdn-Enabled'] == 'True'
        if connection.hp_cdn_ssl == true
          response.headers.fetch('X-Cdn-Ssl-Uri', nil)
        else
          response.headers.fetch('X-Cdn-Uri', nil)
        end
      end
    rescue Fog::CDN::HP::NotFound => err
      nil
    end
  end
end

#destroyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/hp/models/storage/directory.rb', line 26

def destroy
  requires :key
  connection.delete_container(key)
  # If CDN service is available, try to delete the container if it was CDN-enabled
  if cdn_enabled?
    begin
      connection.cdn.delete_container(key)
    rescue Fog::CDN::HP::NotFound
      # ignore if cdn container not found
    end
  end
  true
rescue Excon::Errors::NotFound, Fog::Storage::HP::NotFound
  false
end

#filesObject



42
43
44
45
46
47
48
49
# File 'lib/fog/hp/models/storage/directory.rb', line 42

def files
  @files ||= begin
    Fog::Storage::HP::Files.new(
      :directory    => self,
      :connection   => connection
    )
  end
end

#public=(new_public) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/fog/hp/models/storage/directory.rb', line 51

def public=(new_public)
  if new_public
    @acl = 'public-read'
  else
    @acl = 'private'
  end
  @public = new_public
end

#public?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/fog/hp/models/storage/directory.rb', line 60

def public?
  if @acl.nil?
    false
  else
    @acl == 'public-read'
  end
end

#public_urlObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/hp/models/storage/directory.rb', line 68

def public_url
  requires :key
  @public_url ||= begin
    begin response = connection.head_container(key)
      # escape the key to cover for special char. in container names
      url = "#{connection.url}/#{Fog::HP.escape(key)}"
    rescue Fog::Storage::HP::NotFound => err
      nil
    end
  end
end

#saveObject



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
154
# File 'lib/fog/hp/models/storage/directory.rb', line 125

def save
  requires :key
  options = {}
  if @acl
    options.merge!(connection.acl_to_header(@acl))
  end
  connection.put_container(key, options)
  # Added an extra check to see if CDN is enabled for the container
  if (!connection.cdn.nil? && connection.cdn.enabled?)
    # If CDN available, set the container to be CDN-enabled or not based on if it is marked as cdn_enable.
    if @cdn_enable
      # check to make sure that the container exists. If yes, cdn enable it.
      begin response = connection.cdn.head_container(key)
        ### Deleting a container from CDN is much more expensive than flipping the bit to disable it
        connection.cdn.post_container(key, {'X-CDN-Enabled' => 'True'})
      rescue Fog::CDN::HP::NotFound => err
        connection.cdn.put_container(key)
      end
    else
      # check to make sure that the container exists. If yes, cdn disable it.
      begin response = connection.cdn.head_container(key)
        ### Deleting a container from CDN is much more expensive than flipping the bit to disable it
        connection.cdn.post_container(key, {'X-CDN-Enabled' => 'False'})
      rescue Fog::CDN::HP::NotFound => err
        # just continue, as container is not cdn-enabled.
      end
    end
  end
  true
end