Class: Fog::Resources::AzureRM::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/azurerm/resources.rb,
lib/fog/azurerm/requests/resources/tag_resource.rb,
lib/fog/azurerm/requests/resources/get_deployment.rb,
lib/fog/azurerm/requests/resources/list_deployments.rb,
lib/fog/azurerm/requests/resources/create_deployment.rb,
lib/fog/azurerm/requests/resources/delete_deployment.rb,
lib/fog/azurerm/requests/resources/get_resource_group.rb,
lib/fog/azurerm/requests/resources/delete_resource_tag.rb,
lib/fog/azurerm/requests/resources/list_resource_groups.rb,
lib/fog/azurerm/requests/resources/create_resource_group.rb,
lib/fog/azurerm/requests/resources/delete_resource_group.rb,
lib/fog/azurerm/requests/resources/list_tagged_resources.rb,
lib/fog/azurerm/requests/resources/check_deployment_exists.rb,
lib/fog/azurerm/requests/resources/check_azure_resource_exists.rb,
lib/fog/azurerm/requests/resources/check_resource_group_exists.rb,
lib/fog/azurerm/requests/resources/list_resources_in_resource_group.rb

Overview

This class provides the actual implementation for service calls.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real

Returns a new instance of Real.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/azurerm/resources.rb', line 53

def initialize(options)
  begin
    require 'azure_mgmt_resources'
  rescue LoadError => e
    retry if require('rubygems')
    raise e.message
  end

  options[:environment] = 'AzureCloud' if options[:environment].nil?

  credentials = Fog::Credentials::AzureRM.get_credentials(options[:tenant_id], options[:client_id], options[:client_secret], options[:environment])
  telemetry = "fog-azure-rm/#{Fog::AzureRM::VERSION}"
  @rmc = ::Azure::ARM::Resources::ResourceManagementClient.new(credentials, resource_manager_endpoint_url(options[:environment]))
  @rmc.subscription_id = options[:subscription_id]
  @rmc.add_user_agent_information(telemetry)
end

Instance Method Details

#check_azure_resource_exists(resource_id, api_version = API_VERSION) ⇒ Object



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
# File 'lib/fog/azurerm/requests/resources/check_azure_resource_exists.rb', line 6

def check_azure_resource_exists(resource_id, api_version = API_VERSION)
  split_resource = resource_id.split('/') unless resource_id.nil?
  raise 'Invalid Resource Id' if split_resource.count != 9

  resource_group_name = get_resource_from_resource_id(resource_id, RESOURCE_GROUP_NAME)
  resource_provider_namespace = get_resource_from_resource_id(resource_id, RESOURCE_PROVIDER_NAMESPACE)
  resource_type = get_resource_from_resource_id(resource_id, RESOURCE_TYPE)
  resource_name = get_resource_from_resource_id(resource_id, RESOURCE_NAME)
  parent_resource_id = ''

  msg = "Checking Resource #{resource_name}"
  Fog::Logger.debug msg
  begin
    resource_exists = @rmc.resources.check_existence(resource_group_name, resource_provider_namespace, parent_resource_id, resource_type, resource_name, api_version)
    Fog::Logger.debug "Resource #{resource_name} exists." if resource_exists
    resource_exists
  rescue MsRestAzure::AzureOperationError => e
    if e.response.status == 405
      Fog::Logger.debug "Resource #{resource_name} doesn't exist."
      false
    else
      raise_azure_exception(e, msg)
    end
  end
end

#check_deployment_exists(resource_group_name, deployment_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/azurerm/requests/resources/check_deployment_exists.rb', line 6

def check_deployment_exists(resource_group_name, deployment_name)
  msg = "Checking Deployment #{deployment_name}"
  Fog::Logger.debug msg
  begin
    flag = @rmc.deployments.check_existence(resource_group_name, deployment_name)
    if flag
      Fog::Logger.debug "Deployment #{deployment_name} exists."
    else
      Fog::Logger.debug "Deployment #{deployment_name} doesn't exist."
    end
    flag
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
end

#check_resource_group_exists(resource_group_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/azurerm/requests/resources/check_resource_group_exists.rb', line 6

def check_resource_group_exists(resource_group_name)
  msg = "Checking Resource Group #{resource_group_name}"
  Fog::Logger.debug msg
  begin
    flag = @rmc.resource_groups.check_existence(resource_group_name)
    if flag
      Fog::Logger.debug "Resource Group #{resource_group_name} exists."
    else
      Fog::Logger.debug "Resource Group #{resource_group_name} doesn't exist."
    end
    flag
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
end

#create_deployment(resource_group, deployment_name, template_link, parameters_link) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/azurerm/requests/resources/create_deployment.rb', line 6

def create_deployment(resource_group, deployment_name, template_link, parameters_link)
  msg = "Creating Deployment: #{deployment_name} in Resource Group: #{resource_group}"
  Fog::Logger.debug msg
  deployment = create_deployment_object(template_link, parameters_link)
  begin
    @rmc.deployments.validate(resource_group, deployment_name, deployment)
    deployment = @rmc.deployments.create_or_update(resource_group, deployment_name, deployment)
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Deployment: #{deployment_name} in Resource Group: #{resource_group} created successfully."
  deployment
end

#create_resource_group(name, location, tags) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/azurerm/requests/resources/create_resource_group.rb', line 6

def create_resource_group(name, location, tags)
  msg = "Creating Resource Group: #{name}."
  Fog::Logger.debug msg
  resource_group = Azure::ARM::Resources::Models::ResourceGroup.new
  resource_group.location = location
  resource_group.tags = tags
  begin
    resource_group = @rmc.resource_groups.create_or_update(name, resource_group)
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Resource Group #{name} created successfully."
  resource_group
end

#delete_deployment(resource_group, deployment_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/resources/delete_deployment.rb', line 6

def delete_deployment(resource_group, deployment_name)
  msg = "Deleting Deployment: #{deployment_name} in Resource Group: #{resource_group}"
  Fog::Logger.debug msg
  begin
    @rmc.deployments.delete(resource_group, deployment_name)
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Deployment: #{deployment_name} in Resource Group: #{resource_group} deleted successfully."
  true
end

#delete_resource_group(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/resources/delete_resource_group.rb', line 6

def delete_resource_group(name)
  msg = "Deleting Resource Group: #{name}."
  Fog::Logger.debug msg
  begin
    @rmc.resource_groups.delete(name)
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Resource Group #{name} deleted successfully."
  true
end

#delete_resource_tag(resource_id, tag_name, tag_value, api_version = API_VERSION) ⇒ Object



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
# File 'lib/fog/azurerm/requests/resources/delete_resource_tag.rb', line 6

def delete_resource_tag(resource_id, tag_name, tag_value, api_version = API_VERSION)
  split_resource = resource_id.split('/') unless resource_id.nil?
  if split_resource.count != 9
    raise 'Invalid Resource Id'
  end

  resource_group_name = get_resource_from_resource_id(resource_id, RESOURCE_GROUP_NAME)
  resource_provider_namespace = get_resource_from_resource_id(resource_id, RESOURCE_PROVIDER_NAMESPACE)
  resource_type = get_resource_from_resource_id(resource_id, RESOURCE_TYPE)
  resource_name = get_resource_from_resource_id(resource_id, RESOURCE_NAME)

  msg = "Deleting Tag #{tag_name} from Resource #{resource_name}"
  Fog::Logger.debug msg

  begin
    resource = @rmc.resources.get(resource_group_name, resource_provider_namespace, '', resource_type, resource_name, api_version)

    if resource.tags.key?(tag_name)
      resource.tags.delete_if { |key, value| key == tag_name && value == tag_value }
    end
    @rmc.resources.create_or_update(resource_group_name, resource_provider_namespace, '', resource_type, resource_name, api_version, resource)
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Tag #{tag_name} deleted successfully from Resource #{resource_name}"
  true
end

#get_deployment(resource_group, deployment_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/resources/get_deployment.rb', line 6

def get_deployment(resource_group, deployment_name)
  msg = "Getting Deployment #{deployment_name} in Resource Group: #{resource_group}"
  Fog::Logger.debug msg
  begin
    deployment = @rmc.deployments.get(resource_group, deployment_name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Getting deployment #{deployment_name} successfully in Resource Group: #{resource_group}"
  deployment
end

#get_resource_group(resource_group_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/resources/get_resource_group.rb', line 6

def get_resource_group(resource_group_name)
  msg = "Getting Resource Group #{resource_group_name}"
  Fog::Logger.debug msg
  begin
    resource_group = @rmc.resource_groups.get(resource_group_name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Getting Resource Group #{resource_group_name} successfully"
  resource_group
end

#list_deployments(resource_group) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/resources/list_deployments.rb', line 6

def list_deployments(resource_group)
  msg = "Listing Deployments in Resource Group: #{resource_group}"
  Fog::Logger.debug msg
  begin
    deployments = @rmc.deployments.list_as_lazy(resource_group)
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Deployments listed successfully in Resource Group: #{resource_group}"
  deployments.value
end

#list_resource_groupsObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fog/azurerm/requests/resources/list_resource_groups.rb', line 6

def list_resource_groups
  msg = 'Listing Resource Groups'
  Fog::Logger.debug msg
  begin
    resource_groups = @rmc.resource_groups.list_as_lazy
  rescue  MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  resource_groups.next_link = '' if resource_groups.next_link.nil?
  Fog::Logger.debug 'Resource Groups listed successfully'
  resource_groups.value
end

#list_resources_in_resource_group(resource_group_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/resources/list_resources_in_resource_group.rb', line 6

def list_resources_in_resource_group(resource_group_name)
  msg = "Listing Resources in #{resource_group_name}"
  Fog::Logger.debug msg
  begin
    resources = @rmc.resource_groups.list_resources(resource_group_name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Resources in #{resource_group_name} listed successfully"
  resources
end

#list_tagged_resources(tag_name, tag_value = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/azurerm/requests/resources/list_tagged_resources.rb', line 6

def list_tagged_resources(tag_name, tag_value = nil)
  msg = "Listing Resources with tagname: #{tag_name}"
  Fog::Logger.debug msg
  unless tag_name.nil?
    query_filter = "tagname eq '#{tag_name}' "
    query_filter += tag_value.nil? ? '' : "and tagvalue eq '#{tag_value}'"
    begin
      resources = @rmc.resources.list_as_lazy(query_filter)
    rescue MsRestAzure::AzureOperationError => e
      raise_azure_exception(e, msg)
    end
    resources.next_link = '' if resources.next_link.nil?
    resources.value
  end
end

#tag_resource(resource_id, tag_name, tag_value, api_version = API_VERSION) ⇒ Object



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
# File 'lib/fog/azurerm/requests/resources/tag_resource.rb', line 6

def tag_resource(resource_id, tag_name, tag_value, api_version = API_VERSION)
  split_resource = resource_id.split('/') unless resource_id.nil?
  if split_resource.count != 9
    raise 'Invalid Resource Id'
  end

  resource_group_name = get_resource_from_resource_id(resource_id, RESOURCE_GROUP_NAME)
  resource_provider_namespace = get_resource_from_resource_id(resource_id, RESOURCE_PROVIDER_NAMESPACE)
  resource_type = get_resource_from_resource_id(resource_id, RESOURCE_TYPE)
  resource_name = get_resource_from_resource_id(resource_id, RESOURCE_NAME)
  parent_resource_id = ''

  msg = "Creating Tag #{tag_name} for Resource #{resource_name}"
  Fog::Logger.debug msg

  begin
    resource = @rmc.resources.get(resource_group_name, resource_provider_namespace, parent_resource_id, resource_type, resource_name, api_version)
    resource.tags = {} if resource.tags.nil?
    resource.tags[tag_name] = tag_value
    @rmc.resources.create_or_update(resource_group_name, resource_provider_namespace, parent_resource_id, resource_type, resource_name, api_version, resource)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Tag #{tag_name} created successfully for Resource #{resource_name}"
  true
end