azure_file_shares 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ module AzureFileShares
2
+ # Configuration options for the AzureFileShares client
3
+ class Configuration
4
+ # API endpoints
5
+ DEFAULT_API_VERSION = "2024-01-01"
6
+ DEFAULT_BASE_URL = "https://management.azure.com"
7
+
8
+ # Configuration attributes
9
+ attr_accessor :tenant_id,
10
+ :client_id,
11
+ :client_secret,
12
+ :subscription_id,
13
+ :resource_group_name,
14
+ :storage_account_name,
15
+ :storage_account_key,
16
+ # :sas_token,
17
+ :api_version,
18
+ :base_url,
19
+ :request_timeout,
20
+ :logger
21
+
22
+ # Initialize a new Configuration object with default values
23
+ def initialize
24
+ @api_version = DEFAULT_API_VERSION
25
+ @base_url = DEFAULT_BASE_URL
26
+ @request_timeout = 60 # seconds
27
+ @logger = nil
28
+ end
29
+
30
+ # Validates the configuration
31
+ # @return [Boolean] true if valid
32
+ # @raise [AzureFileShares::Errors::ConfigurationError] if configuration is invalid
33
+ def validate!
34
+ # For file operations with SAS token
35
+ # if storage_account_name && sas_token
36
+ # return true
37
+ # end
38
+
39
+ # For file operations only
40
+ if storage_account_name && storage_account_key
41
+ return true
42
+ end
43
+
44
+ # For ARM operations (share management)
45
+ required_fields = %i[tenant_id client_id client_secret subscription_id]
46
+ missing_fields = required_fields.select { |field| send(field).nil? || send(field).to_s.strip.empty? }
47
+
48
+ unless missing_fields.empty?
49
+ missing = missing_fields.map(&:to_s).join(", ")
50
+ raise AzureFileShares::Errors::ConfigurationError,
51
+ "Missing required configuration: #{missing}. " +
52
+ "Note: For file operations only, just storage_account_name with either storage_account_key or sas_token is required."
53
+ end
54
+
55
+ true
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,18 @@
1
+ module AzureFileShares
2
+ module Errors
3
+ # Base error class for API-related errors
4
+ class ApiError < StandardError
5
+ attr_reader :status, :response
6
+
7
+ # Initialize a new API error
8
+ # @param [String] message Error message
9
+ # @param [Integer] status HTTP status code
10
+ # @param [Hash] response Full response object
11
+ def initialize(message = nil, status = nil, response = nil)
12
+ @status = status
13
+ @response = response
14
+ super(message)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module AzureFileShares
2
+ module Errors
3
+ # Configuration error class
4
+ class ConfigurationError < StandardError; end
5
+ end
6
+ end
@@ -0,0 +1,90 @@
1
+ module AzureFileShares
2
+ module Operations
3
+ # Base class for all API operations
4
+ class BaseOperation
5
+ attr_reader :client
6
+
7
+ # Initialize a new operation with a client
8
+ # @param [AzureFileShares::Client] client The API client
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ private
14
+
15
+ # Build the base path for storage account operations
16
+ # @return [String] Base path
17
+ def base_path
18
+ "/subscriptions/#{client.subscription_id}" \
19
+ "/resourceGroups/#{client.resource_group_name}" \
20
+ "/providers/Microsoft.Storage" \
21
+ "/storageAccounts/#{client.storage_account_name}"
22
+ end
23
+
24
+ # Build the full request URL
25
+ # @param [String] path The API endpoint path
26
+ # @return [String] Full URL
27
+ def build_url(path, params = {})
28
+ url = URI.join(client.base_url, path).to_s
29
+ url += "?api-version=#{client.api_version}"
30
+
31
+ params.each do |key, value|
32
+ url += "&#{key}=#{URI.encode_www_form_component(value.to_s)}"
33
+ end
34
+
35
+ url
36
+ end
37
+
38
+ # Send a request to the API
39
+ # @param [Symbol] method HTTP method (:get, :post, :put, :delete, etc.)
40
+ # @param [String] path API endpoint path
41
+ # @param [Hash] params Query parameters to include in the URL
42
+ # @param [Hash] body Request body (for POST, PUT, PATCH)
43
+ # @return [Hash] Parsed JSON response
44
+ # @raise [AzureFileShares::Errors::ApiError] If the request fails
45
+ def request(method, path, params = {}, body = nil)
46
+ url = build_url(path, params)
47
+
48
+ response = client.connection.public_send(method) do |req|
49
+ req.url url
50
+ req.headers["Authorization"] = "Bearer #{client.access_token}"
51
+ req.headers["Content-Type"] = "application/json"
52
+ req.body = JSON.generate(body) if body
53
+ end
54
+
55
+ handle_response(response)
56
+ end
57
+
58
+ # Handle the API response, raising errors when necessary
59
+ # @param [Faraday::Response] response The HTTP response
60
+ # @return [Hash, Array] Parsed JSON response
61
+ # @raise [AzureFileShares::Errors::ApiError] If the request fails
62
+ def handle_response(response)
63
+ puts "Response: #{response.inspect}"
64
+ case response.status
65
+ when 200..299
66
+ return {} if response.body.nil? || response.body.empty?
67
+
68
+ begin
69
+ JSON.parse(response.body)
70
+ rescue JSON::ParserError
71
+ response.body
72
+ end
73
+ else
74
+ error_message = begin
75
+ error_data = JSON.parse(response.body)
76
+ error_data.dig("error", "message") || response.reason_phrase
77
+ rescue JSON::ParserError
78
+ response.reason_phrase || "Unknown error"
79
+ end
80
+
81
+ raise AzureFileShares::Errors::ApiError.new(
82
+ "API Error (#{response.status}): #{error_message}",
83
+ response.status,
84
+ response.body
85
+ )
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end