Class: Azure::BaseManagement::BaseManagementService

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/base_management/base_management_service.rb

Overview

The base for all Azure management services

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseManagementService

Returns a new instance of BaseManagementService.

Parameters:

  • options (Hash) (defaults to: {})

    options including :client



34
35
36
37
# File 'lib/azure/base_management/base_management_service.rb', line 34

def initialize(options = {})
  @client = options[:client] || Azure.client
  validate_configuration!
end

Instance Attribute Details

#clientAzure::Client

The client contains the configuration scope and the ability to produce http agents. defaults to global client.

Returns:

See Also:



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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/azure/base_management/base_management_service.rb', line 29

class BaseManagementService

  attr_accessor :client

  # @param options  [Hash] options including :client
  def initialize(options = {})
    @client = options[:client] || Azure.client
    validate_configuration!
  end

  # Validate the configuration of the service.
  # @return [void]
  def validate_configuration!
    subs_id = @client.subscription_id
    error_message = 'Subscription ID not valid.'
    raise ArgumentError.new(error_message) if subs_id.nil? || subs_id.empty?

    m_ep = @client.management_endpoint
    error_message = 'Management endpoint not valid.'
    raise ArgumentError.new(error_message) if m_ep.nil? || m_ep.empty?
  end

  # Gets a list of regional data center locations from the server
  # @return [Array<Azure::BaseManagement::Location>]
  def list_locations
    request = @client.management_request(:get, '/locations')
    response = request.call
    Serialization.locations_from_xml(response)
  end

  # Gets a list of role sizes associated with the
  # specified subscription.
  # @return [Array<String>]
  def list_role_sizes
    role_sizes = []
    locations = list_locations
    locations.each do | location |
     role_sizes << location.role_sizes
    end
    role_sizes.flatten.uniq.compact.sort
  end

  # Gets a lists the affinity groups associated with
  # the specified subscription.
  # @see http://msdn.microsoft.com/en-us/library/azure/ee460797.aspx
  # @return [Array<Azure::BaseManagement::AffinityGroup>]
  def list_affinity_groups
    request_path = '/affinitygroups'
    request = @client.management_request(:get, request_path)
    response = request.call
    Serialization.affinity_groups_from_xml(response)
  end

  # Creates a new affinity group for the specified subscription.
  #
  # ==== Attributes
  #
  # * +name+           - String. Affinity Group name.
  # * +location+       - String. The location where the affinity group will
  # be created.
  # * +label+         - String. Name for the affinity specified as a
  # base-64 encoded string.
  #
  # ==== Options
  #
  # Accepted key/value pairs are:
  # * +:description+   - String. A description for the affinity group.
  # (optional)
  #
  # @see http://msdn.microsoft.com/en-us/library/azure/gg715317.aspx
  #
  # @return [void]
  def create_affinity_group(name, location, label, options = {})
    if name.nil? || name.strip.empty?
      raise 'Affinity Group name cannot be empty'
    elsif list_affinity_groups.map(&:name).include?(name)
      raise Azure::Error::Error.new(
        'ConflictError',
        409,
        "An affinity group #{name} already exists in the current subscription."
      )
    else
      validate_location(location)
      body = Serialization.affinity_group_to_xml(name,
                                                 location,
                                                 label,
                                                 options)
      request_path = '/affinitygroups'
      request = @client.management_request(:post, request_path, body)
      request.call
      Azure::Loggerx.info "Affinity Group #{name} is created."
    end
  end

  # Updates the label and/or the description for an affinity group
  # for the specified subscription.
  #
  # ==== Attributes
  #
  # * +name+          - String. Affinity Group name.
  # * +label+         - String. Name for the affinity specified as a
  # base-64 encoded string.
  #
  # ==== Options
  #
  # Accepted key/value pairs are:
  # * +:description+   - String. A description for the affinity group.
  # (optional)
  #
  # @see http://msdn.microsoft.com/en-us/library/azure/gg715316.aspx
  #
  # @return [void]
  def update_affinity_group(name, label, options = {})
    raise 'Label name cannot be empty' if label.nil? || label.empty?
    if affinity_group(name)
      body = Serialization.resource_to_xml(label, options)
      request_path = "/affinitygroups/#{name}"
      request = @client.management_request(:put, request_path, body)
      request.call
      Azure::Loggerx.info "Affinity Group #{name} is updated."
    end
  end

  # Deletes an affinity group in the specified subscription
  #
  # ==== Attributes
  #
  # * +name+       - String. Affinity Group name.
  #
  # @see http://msdn.microsoft.com/en-us/library/azure/gg715314.aspx
  #
  # @return [void]
  def delete_affinity_group(name)
    if affinity_group(name)
      request_path = "/affinitygroups/#{name}"
      request = @client.management_request(:delete, request_path)
      request.call
      Azure::Loggerx.info "Deleted affinity group #{name}."
    end
  end

  # Returns the system properties associated with the specified
  # affinity group.
  #
  # ==== Attributes
  #
  # * +name+       - String. Affinity Group name.
  #
  # @see http://msdn.microsoft.com/en-us/library/azure/ee460789.aspx
  #
  # @return  [Azure::BaseManagement::AffinityGroup]
  def get_affinity_group(name)
    if affinity_group(name)
      request_path = "/affinitygroups/#{name}"
      request = @client.management_request(:get, request_path)
      response = request.call
      Serialization.affinity_group_from_xml(response)
    end
  end

  private

  def affinity_group(affinity_group_name)
    if affinity_group_name.nil? ||\
       affinity_group_name.empty? ||\
       !list_affinity_groups.map { |x| x.name.downcase }.include?(
        affinity_group_name.downcase
       )
      error = Azure::Error::Error.new('AffinityGroupNotFound',
                                      404,
                                      'The affinity group does not exist.')
      raise error
    else
      true
    end
  end

  def validate_location(location_name)
    base_mgmt_service = Azure::BaseManagementService.new
    locations = base_mgmt_service.list_locations.map(&:name)
    unless locations.map(&:downcase).include?(location_name.downcase)
      error = "Value '#{location_name}' specified for parameter"\
              " 'location' is invalid."\
              " Allowed values are #{locations.join(',')}"
      raise error
    end
  end
end

Instance Method Details

#create_affinity_group(name, location, label, options = {}) ⇒ void

This method returns an undefined value.

Creates a new affinity group for the specified subscription.

Attributes

  • name - String. Affinity Group name.

  • location - String. The location where the affinity group will

be created.

  • label - String. Name for the affinity specified as a

base-64 encoded string.

Options

Accepted key/value pairs are:

  • :description - String. A description for the affinity group.

(optional)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/azure/base_management/base_management_service.rb', line 101

def create_affinity_group(name, location, label, options = {})
  if name.nil? || name.strip.empty?
    raise 'Affinity Group name cannot be empty'
  elsif list_affinity_groups.map(&:name).include?(name)
    raise Azure::Error::Error.new(
      'ConflictError',
      409,
      "An affinity group #{name} already exists in the current subscription."
    )
  else
    validate_location(location)
    body = Serialization.affinity_group_to_xml(name,
                                               location,
                                               label,
                                               options)
    request_path = '/affinitygroups'
    request = @client.management_request(:post, request_path, body)
    request.call
    Azure::Loggerx.info "Affinity Group #{name} is created."
  end
end

#delete_affinity_group(name) ⇒ void

This method returns an undefined value.

Deletes an affinity group in the specified subscription

Attributes

  • name - String. Affinity Group name.



161
162
163
164
165
166
167
168
# File 'lib/azure/base_management/base_management_service.rb', line 161

def delete_affinity_group(name)
  if affinity_group(name)
    request_path = "/affinitygroups/#{name}"
    request = @client.management_request(:delete, request_path)
    request.call
    Azure::Loggerx.info "Deleted affinity group #{name}."
  end
end

#get_affinity_group(name) ⇒ Azure::BaseManagement::AffinityGroup

Returns the system properties associated with the specified affinity group.

Attributes

  • name - String. Affinity Group name.



180
181
182
183
184
185
186
187
# File 'lib/azure/base_management/base_management_service.rb', line 180

def get_affinity_group(name)
  if affinity_group(name)
    request_path = "/affinitygroups/#{name}"
    request = @client.management_request(:get, request_path)
    response = request.call
    Serialization.affinity_group_from_xml(response)
  end
end

#list_affinity_groupsArray<Azure::BaseManagement::AffinityGroup>

Gets a lists the affinity groups associated with the specified subscription.



75
76
77
78
79
80
# File 'lib/azure/base_management/base_management_service.rb', line 75

def list_affinity_groups
  request_path = '/affinitygroups'
  request = @client.management_request(:get, request_path)
  response = request.call
  Serialization.affinity_groups_from_xml(response)
end

#list_locationsArray<Azure::BaseManagement::Location>

Gets a list of regional data center locations from the server



53
54
55
56
57
# File 'lib/azure/base_management/base_management_service.rb', line 53

def list_locations
  request = @client.management_request(:get, '/locations')
  response = request.call
  Serialization.locations_from_xml(response)
end

#list_role_sizesArray<String>

Gets a list of role sizes associated with the specified subscription.

Returns:

  • (Array<String>)


62
63
64
65
66
67
68
69
# File 'lib/azure/base_management/base_management_service.rb', line 62

def list_role_sizes
  role_sizes = []
  locations = list_locations
  locations.each do | location |
   role_sizes << location.role_sizes
  end
  role_sizes.flatten.uniq.compact.sort
end

#update_affinity_group(name, label, options = {}) ⇒ void

This method returns an undefined value.

Updates the label and/or the description for an affinity group for the specified subscription.

Attributes

  • name - String. Affinity Group name.

  • label - String. Name for the affinity specified as a

base-64 encoded string.

Options

Accepted key/value pairs are:

  • :description - String. A description for the affinity group.

(optional)



141
142
143
144
145
146
147
148
149
150
# File 'lib/azure/base_management/base_management_service.rb', line 141

def update_affinity_group(name, label, options = {})
  raise 'Label name cannot be empty' if label.nil? || label.empty?
  if affinity_group(name)
    body = Serialization.resource_to_xml(label, options)
    request_path = "/affinitygroups/#{name}"
    request = @client.management_request(:put, request_path, body)
    request.call
    Azure::Loggerx.info "Affinity Group #{name} is updated."
  end
end

#validate_configuration!void

This method returns an undefined value.

Validate the configuration of the service.

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
# File 'lib/azure/base_management/base_management_service.rb', line 41

def validate_configuration!
  subs_id = @client.subscription_id
  error_message = 'Subscription ID not valid.'
  raise ArgumentError.new(error_message) if subs_id.nil? || subs_id.empty?

  m_ep = @client.management_endpoint
  error_message = 'Management endpoint not valid.'
  raise ArgumentError.new(error_message) if m_ep.nil? || m_ep.empty?
end