Skip to content

Resource group faraday #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: rest-api-migration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/fog/azurerm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'fog/azurerm/models/storage/sku_name'
require 'fog/azurerm/models/storage/sku_tier'
require 'fog/azurerm/models/storage/kind'
require 'faraday'

module Fog
# Autoload Module for Credentials
Expand Down Expand Up @@ -68,9 +69,11 @@ module KeyVault
autoload :AzureRM, File.expand_path('azurerm/key_vault', __dir__)
end

# Autoload Module for Response::Asynchronous
# Autoload Module for Response::Asynchronous, NetworkAdapter and CustomException
module AzureRM
autoload :AsyncResponse, File.expand_path('azurerm/async_response', __dir__)
autoload :NetworkAdapter, File.expand_path('azurerm/network_adapter', __dir__)
autoload :CustomException, File.expand_path('azurerm/custom_exception', __dir__)
end

# Main AzureRM fog Provider Module
Expand Down
12 changes: 12 additions & 0 deletions lib/fog/azurerm/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@
TEMPORARY_STORAGE_ACCOUNT_TAG_VALUE = 'delete'.freeze

HTTP_NOT_FOUND = 404

GET_METHOD = 'get'.freeze
PUT_METHOD = 'put'.freeze
DELETE_METHOD = 'delete'.freeze

SUCCESS = 'success'.freeze
FAILURE = 'failure'.freeze

STATUS_CODES = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

success: [200, 201, 202, 203, 204],
failure: [400, 401, 402, 403, 404, 500, 501, 502, 503]
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final newline missing.

32 changes: 32 additions & 0 deletions lib/fog/azurerm/custom_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Fog
module AzureRM
class CustomException < StandardError
attr_accessor :request
attr_accessor :response
attr_accessor :body
attr_accessor :error_message
attr_accessor :error_code

def initialize(exception)
@request = exception.env.request
@response = exception.env.response
@body = JSON.decode(exception.env.body)
@error_message = @body['error']['message']
@error_code = @body['error']['code']
super
end

def to_s
exception = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

exception['message'] = @error_message
exception['code'] = @error_code
exception['body'] = @body
exception['request'] = @request
exception['response'] = @response

Fog::JSON.encode(exception)
end
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final newline missing.

30 changes: 30 additions & 0 deletions lib/fog/azurerm/network_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Fog
module AzureRM
class NetworkAdapter
def self.get(url, token)
send_request(url, token, nil, GET_METHOD)
end

def self.put(url, token, body)
send_request(url, token, body, PUT_METHOD)
end

def self.delete(url, token)
send_request(url, token, nil, DELETE_METHOD)
end

private

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless private access modifier.


def self.send_request(url, token, body, method_type)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private (on line 16) does not make singleton methods private. Use private_class_method or private inside a class << self block instead.

connection = Faraday.new

response = connection.send(method_type, url) do |request|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless assignment to variable - response.

request.headers['accept'] = 'application/json'
request.headers['Content-type'] = 'application/json'
request.headers['authorization'] = token
request.body = body.nil? ? '{}' : body
end
end
end
end
end
25 changes: 15 additions & 10 deletions lib/fog/azurerm/requests/resources/check_resource_group_exists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ class Real
def check_resource_group_exists(resource_group_name)
msg = "Checking Resource Group #{resource_group_name}"
Fog::Logger.debug msg

url = "subscriptions/#{@subscription_id}/resourcegroups/#{resource_group_name}?api-version=2017-05-10"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

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)
response = Fog::AzureRM::NetworkAdapter.get(url, @token)
rescue => e

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid rescuing without specifying an error class.

raise e
end

response_status = parse_response(response)

if response_status.eql?(SUCCESS)
response.env.body
else
raise Fog::AzureRM::CustomException(response)
end
end
end
Expand All @@ -27,4 +32,4 @@ def check_resource_group_exists(*)
end
end
end
end
end
35 changes: 27 additions & 8 deletions lib/fog/azurerm/requests/resources/create_resource_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,35 @@ class Real
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

body = create_resource_group_body(location, tags).to_json

url = "subscriptions/#{@subscription_id}/resourcegroups/#{name}?api-version=2017-05-10"
begin
resource_group = @rmc.resource_groups.create_or_update(name, resource_group)
rescue MsRestAzure::AzureOperationError => e
raise_azure_exception(e, msg)
response = Fog::AzureRM::NetworkAdapter.put(
url,
@token,
body
)
rescue => e

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid rescuing without specifying an error class.

raise e
end

response_status = parse_response(response)

if response_status.eql?(SUCCESS)
Fog::Logger.debug "Resource Group #{name} created successfully."
response.env.body
else
raise Fog::AzureRM::CustomException.new(response)
end
Fog::Logger.debug "Resource Group #{name} created successfully."
resource_group
end

def create_resource_group_body(location, tags)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused method argument - tags. If it's necessary, use _ or _tags as an argument name to indicate that it won't be used.

parameters = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless assignment to variable - parameters.

location: location,
properties: {}
}
end
end

Expand Down
12 changes: 9 additions & 3 deletions lib/fog/azurerm/requests/resources/delete_resource_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ class Real
def delete_resource_group(name)
msg = "Deleting Resource Group: #{name}."
Fog::Logger.debug msg

url = "subscriptions/#{@subscription_id}/resourcegroups/#{name}?api-version=2017-05-10"

begin
@rmc.resource_groups.delete(name)
rescue MsRestAzure::AzureOperationError => e
raise_azure_exception(e, msg)
Fog::AzureRM::NetworkAdapter.delete(
url,
@token
)
rescue => e

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid rescuing without specifying an error class.

raise e
end
Fog::Logger.debug "Resource Group #{name} deleted successfully."
true
Expand Down
11 changes: 6 additions & 5 deletions lib/fog/azurerm/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ def initialize(options)

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)
@tenant_id = options[:tenant_id]
@client_id = options[:client_id]
@subscription_id = options[:subscription_id]
@client_secret = options[:client_secret]

@token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/fog/azurerm/utilities/general.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,8 @@ def resource_not_found?(azure_operation_error)
def get_image_name(id)
id.split('/').last
end

def parse_response(response)
status_code = response.env.status
STATUS_CODES[:success].include? status_code ? SUCCESS : FAILURE
end