-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
base: rest-api-migration
Are you sure you want to change the base?
Changes from all commits
1252ca7
155c1c3
0f8ada7
fa2c5a9
54092fe
81a16f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
success: [200, 201, 202, 203, 204], | ||
failure: [400, 401, 402, 403, 404, 500, 501, 502, 503] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Final newline missing. |
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 = {} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Final newline missing. |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -27,4 +32,4 @@ def check_resource_group_exists(*) | |
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless assignment to variable - parameters. |
||
location: location, | ||
properties: {} | ||
} | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.