azure-armrest 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,94 +2,15 @@ module Azure
2
2
  module Armrest
3
3
  module Network
4
4
  # Class for managing network security groups.
5
- class NetworkSecurityGroupService < ArmrestService
5
+ class NetworkSecurityGroupService < ResourceGroupBasedService
6
6
 
7
7
  # Creates and returns a new NetworkSecurityGroupService instance.
8
8
  #
9
9
  def initialize(_armrest_configuration, options = {})
10
10
  super
11
11
  @provider = options[:provider] || 'Microsoft.Network'
12
- set_service_api_version(options, 'networkSecurityGroups')
13
- end
14
-
15
- # Return information for the given network security group name for the
16
- # provided +resource_group+. If no group is specified, it will use the
17
- # resource group set in the constructor.
18
- #
19
- # Example:
20
- #
21
- # # Where 'your_security_group' is likely same as the name of a VM.
22
- # nsg.get('your_security_group', 'your_resource_group')
23
- #
24
- def get(ns_group_name, resource_group = armrest_configuration.resource_group)
25
- raise ArgumentError, "must specify resource group" unless resource_group
26
- url = build_url(resource_group, ns_group_name)
27
- JSON.parse(rest_get(url))
28
- end
29
-
30
- # Returns a list of available network security groups for the given subscription
31
- # for the provided +group+, or for all resource groups if no group is specified.
32
- #
33
- def list(group = nil)
34
- if group
35
- url = build_url(group)
36
- JSON.parse(rest_get(url))['value']
37
- else
38
- array = []
39
- threads = []
40
- mutex = Mutex.new
41
-
42
- resource_groups.each do |rg|
43
- threads << Thread.new(rg['name']) do |group|
44
- url = build_url(group)
45
- response = rest_get(url)
46
- results = JSON.parse(response)['value']
47
- if results && !results.empty?
48
- mutex.synchronize{
49
- results.each{ |hash| hash['resourceGroup'] = group }
50
- array << results
51
- }
52
- end
53
- end
54
- end
55
-
56
- threads.each(&:join)
57
-
58
- array.flatten
59
- end
60
- end
61
-
62
- # List all network security groups for the current subscription.
63
- #
64
- def list_all_for_subscription
65
- sub_id = armrest_configuration.subscription_id
66
- url = File.join(
67
- Azure::Armrest::COMMON_URI, sub_id, 'providers',
68
- @provider, 'networkSecurityGroups'
69
- )
70
- url << "?api-version=#{@api_version}"
71
- JSON.parse(rest_get(url))['value']
72
- end
73
-
74
- alias list_all list_all_for_subscription
75
-
76
- private
77
-
78
- # Builds a URL based on subscription_id an resource_group and any other
79
- # arguments provided, and appends it with the api-version.
80
- def build_url(resource_group, *args)
81
- url = File.join(
82
- Azure::Armrest::COMMON_URI,
83
- armrest_configuration.subscription_id,
84
- 'resourceGroups',
85
- resource_group,
86
- 'providers',
87
- @provider,
88
- 'networkSecurityGroups',
89
- )
90
-
91
- url = File.join(url, *args) unless args.empty?
92
- url << "?api-version=#{@api_version}"
12
+ @service_name = 'networkSecurityGroups'
13
+ set_service_api_version(options, @service_name)
93
14
  end
94
15
  end
95
16
  end # Network
@@ -3,16 +3,6 @@ module Azure
3
3
  module Network
4
4
  # Base class for managing subnets
5
5
  class SubnetService < VirtualNetworkService
6
-
7
- # Create and return a new SubnetService instance. Most methods for a
8
- # SubnetService instance will return one or Subnet instances.
9
- #
10
- def initialize(_armrest_configuration, options = {})
11
- super
12
- @provider = options[:provider] || 'Microsoft.Network'
13
- set_service_api_version(options, 'virtualNetworks')
14
- end
15
-
16
6
  # Creates a new +subnet_name+ on +virtual_network+ using the given
17
7
  # +options+. The +options+ argument is a hash that supports the
18
8
  # following keys and subkeys.
@@ -24,13 +14,8 @@ module Azure
24
14
  # - :routeTable
25
15
  # - :id
26
16
  #
27
- def create(subnet_name, virtual_network, options = {}, resource_group = armrest_configuration.resource_group)
28
- resource_group = options.delete(:resource_group) || resource_group
29
- raise ArgumentError, "no resource group provided" unless resource_group
30
- url = build_url(resource_group, virtual_network, subnet_name)
31
- body = options.to_json
32
- response = rest_put(url, body)
33
- response.return!
17
+ def create(subnet_name, virtual_network, resource_group = armrest_configuration.resource_group, options = {})
18
+ super(combine(virtual_network, subnet_name), resource_group, options)
34
19
  end
35
20
 
36
21
  alias update create
@@ -38,33 +23,33 @@ module Azure
38
23
  # Deletes the given +subnet_name+ in +virtual_network+.
39
24
  #
40
25
  def delete(subnet_name, virtual_network, resource_group = armrest_configuration.resource_group)
41
- raise ArgumentError, "no resource group provided" unless resource_group
42
- url = build_url(resource_group, virtual_network, subnet_name)
43
- response = rest_delete(url)
44
- response.return!
26
+ super(combine(virtual_network, subnet_name), resource_group)
45
27
  end
46
28
 
47
29
  # Retrieves information for the provided +subnet_name+ in +virtual_network+ for
48
30
  # the current subscription.
49
31
  #
50
32
  def get(subnet_name, virtual_network, resource_group = armrest_configuration.resource_group)
51
- raise ArgumentError, "no resource group provided" unless resource_group
52
- url = build_url(resource_group, virtual_network, subnet_name)
53
- JSON.parse(rest_get(url))
33
+ super(combine(virtual_network, subnet_name), resource_group)
54
34
  end
55
35
 
56
36
  # List available subnets on +virtual_network+ for the given +resource_group+.
57
37
  #
58
38
  def list(virtual_network, resource_group = armrest_configuration.resource_group)
59
- raise ArgumentError, "no resource group provided" unless resource_group
60
- url = build_url(resource_group, virtual_network)
61
- JSON.parse(rest_get(url))['value']
39
+ raise ArgumentError, "must specify resource group" unless resource_group
40
+ raise ArgumentError, "must specify name of the resource" unless virtual_network
41
+
42
+ url = build_url(resource_group, virtual_network, 'subnets')
43
+ response = rest_get(url)
44
+ JSON.parse(response)['value'].map{ |hash| model_class.new(hash) }
62
45
  end
63
46
 
47
+ alias list_all list
48
+
64
49
  private
65
50
 
66
- def build_url(resource_group, virtual_network_name, *args)
67
- super(resource_group, virtual_network_name, 'subnets', *args)
51
+ def combine(virtual_newtork, subnet)
52
+ File.join(virtual_newtork, 'subnets', subnet)
68
53
  end
69
54
  end
70
55
  end # Network
@@ -2,112 +2,15 @@ module Azure
2
2
  module Armrest
3
3
  module Network
4
4
  # Class for managing virtual networks.
5
- class VirtualNetworkService < ArmrestService
5
+ class VirtualNetworkService < ResourceGroupBasedService
6
6
 
7
7
  # Creates and returns a new VirtualNetworkService instance.
8
8
  #
9
9
  def initialize(_armrest_configuration, options = {})
10
10
  super
11
11
  @provider = options[:provider] || 'Microsoft.Network'
12
- set_service_api_version(options, 'virtualNetworks')
13
- end
14
-
15
- # Return information for the given virtual network for the provided
16
- # +resource_group+. If no group is specified, it will use the resource
17
- # group set in the constructor.
18
- #
19
- # Example:
20
- #
21
- # vns.get('vn_name', 'your_resource_group')
22
- #
23
- def get(vn_name, resource_group = armrest_configuration.resource_group)
24
- raise ArgumentError, "must specify resource group" unless resource_group
25
- url = build_url(resource_group, vn_name)
26
- JSON.parse(rest_get(url))
27
- end
28
-
29
- # Returns a list of available virtual networks in the current subscription
30
- # for the provided +resource_group+.
31
- #
32
- def list(resource_group = armrest_configuration.resource_group)
33
- raise ArgumentError, "no resource group provided" unless resource_group
34
- url = build_url(resource_group)
35
- JSON.parse(rest_get(url))['value']
36
- end
37
-
38
- # List all virtual networks for the current subscription.
39
- #
40
- def list_all_for_subscription
41
- sub_id = armrest_configuration.subscription_id
42
- url = File.join(Azure::Armrest::COMMON_URI, sub_id, 'providers', @provider, 'virtualNetworks')
43
- url << "?api-version=#{@api_version}"
44
- JSON.parse(rest_get(url))['value']
45
- end
46
-
47
- alias list_all list_all_for_subscription
48
-
49
- # Delete the given virtual network in +resource_group+.
50
- #
51
- def delete(vn_name, resource_group = armrest_configuration.resource_group)
52
- raise ArgumentError, "must specify resource group" unless resource_group
53
-
54
- url = build_url(resource_group, vn_name)
55
- response = rest_delete(url)
56
- response.return!
57
- end
58
-
59
- # Create a new virtual network, or update an existing virtual network if it
60
- # already exists. The first argument is a hash of options.
61
- #
62
- # - :location
63
- # - :tags
64
- # - :etag
65
- # - :properties
66
- # - :addressSpace
67
- # - :addressPrefixes[]
68
- # - :dhcpOptions
69
- # - :dnsServers[]
70
- # - :subnets
71
- # [
72
- # - :name
73
- # - :properties
74
- # - :addressPrefix
75
- # - :networkSecurityGroup
76
- # - :id
77
- # ]
78
- #
79
- def create(vn_name, options, resource_group = armrest_configuration.resource_group)
80
- resource_group = options.delete(:resource_group) || resource_group
81
-
82
- raise ArgumentError, "no resource group specified" unless resource_group
83
-
84
- body = options.to_json
85
-
86
- url = build_url(resource_group, vn_name)
87
-
88
- response = rest_put(url, body)
89
- response.return!
90
- end
91
-
92
- alias update create
93
-
94
- private
95
-
96
- # Builds a URL based on subscription_id an resource_group and any other
97
- # arguments provided, and appends it with the api-version.
98
- def build_url(resource_group, *args)
99
- url = File.join(
100
- Azure::Armrest::COMMON_URI,
101
- armrest_configuration.subscription_id,
102
- 'resourceGroups',
103
- resource_group,
104
- 'providers',
105
- @provider,
106
- 'virtualNetworks',
107
- )
108
-
109
- url = File.join(url, *args) unless args.empty?
110
- url << "?api-version=#{@api_version}"
12
+ @service_name = 'virtualNetworks'
13
+ set_service_api_version(options, @service_name)
111
14
  end
112
15
  end
113
16
  end # Network
@@ -0,0 +1,94 @@
1
+ module Azure::Armrest
2
+ # Base class for services that needs to run in a resource group
3
+ class ResourceGroupBasedService < ArmrestService
4
+ def create(name, rgroup = armrest_configuration.resource_group, options = {})
5
+ raise ArgumentError, "must specify resource group" unless rgroup
6
+ raise ArgumentError, "must specify name of the resource" unless name
7
+
8
+ url = build_url(rgroup, name)
9
+ url = yield(url) || url if block_given?
10
+ response = rest_put(build_url(rgroup, name), options.to_json)
11
+ model_class.new(response) unless response.empty?
12
+ end
13
+
14
+ alias update create
15
+
16
+ def list(rgroup = armrest_configuration.resource_group)
17
+ raise ArgumentError, "must specify resource group" unless rgroup
18
+
19
+ url = build_url(rgroup)
20
+ url = yield(url) || url if block_given?
21
+ response = rest_get(url)
22
+ JSON.parse(response)['value'].map{ |hash| model_class.new(hash) }
23
+ end
24
+
25
+ def list_all
26
+ url = build_url
27
+ url = yield(url) || url if block_given?
28
+ response = rest_get(url)
29
+ JSON.parse(response)['value'].map{ |hash| model_class.new(hash) }
30
+ end
31
+
32
+ def get(name, rgroup = armrest_configuration.resource_group)
33
+ raise ArgumentError, "must specify resource group" unless rgroup
34
+ raise ArgumentError, "must specify name of the resource" unless name
35
+
36
+ url = build_url(rgroup, name)
37
+ url = yield(url) || url if block_given?
38
+ response = rest_get(url)
39
+ model_class.new(response)
40
+ end
41
+
42
+ def delete(name, rgroup= armrest_configuration.resource_group)
43
+ raise ArgumentError, "must specify resource group" unless rgroup
44
+ raise ArgumentError, "must specify name of the resource" unless name
45
+
46
+ url = build_url(rgroup, name)
47
+ url = yield(url) || url if block_given?
48
+ rest_delete(url)
49
+ nil
50
+ end
51
+
52
+ private
53
+
54
+ # Builds a URL based on subscription_id an resource_group and any other
55
+ # arguments provided, and appends it with the api_version.
56
+ #
57
+ def build_url(resource_group = nil, *args)
58
+ url = File.join(Azure::Armrest::COMMON_URI, armrest_configuration.subscription_id)
59
+ url = File.join(url, 'resourceGroups', resource_group) if resource_group
60
+ url = File.join(url, 'providers', @provider, @service_name)
61
+ url = File.join(url, *args) unless args.empty?
62
+ url << "?api-version=#{@api_version}"
63
+ end
64
+
65
+ def model_class
66
+ @model_class ||= Object.const_get(self.class.to_s.sub(/Service$/, ''))
67
+ end
68
+
69
+ # Aggregate resources from each group
70
+ # To be used in the case that API does not support list_all with one call
71
+ def list_in_all_groups
72
+ array = []
73
+ threads = []
74
+ mutex = Mutex.new
75
+
76
+ resource_groups.each do |rg|
77
+ threads << Thread.new(rg['name']) do |group|
78
+ url = build_url(group)
79
+ response = rest_get(url)
80
+
81
+ results = JSON.parse(response)['value'].map { |hash| model_class.new(hash) }
82
+
83
+ if results && !results.empty?
84
+ mutex.synchronize{ array << results }
85
+ end
86
+ end
87
+ end
88
+
89
+ threads.each(&:join)
90
+
91
+ array.flatten
92
+ end
93
+ end
94
+ end
@@ -31,7 +31,7 @@ module Azure
31
31
 
32
32
  response = rest_get(URI.escape(url))
33
33
 
34
- JSON.parse(response.body)["value"]
34
+ JSON.parse(response)["value"].map{ |hash| Azure::Armrest::ResourceGroup.new(hash) }
35
35
  end
36
36
 
37
37
  # Creates a new +group+ in +location+ for the current subscription.
@@ -43,7 +43,7 @@ module Azure
43
43
 
44
44
  response = rest_put(url, body)
45
45
 
46
- JSON.parse(response.body)
46
+ Azure::Armrest::ResourceGroup.new(response)
47
47
  end
48
48
 
49
49
  # Delete a resource group from the current subscription.
@@ -59,7 +59,7 @@ module Azure
59
59
  def get(group)
60
60
  url = build_url(group)
61
61
  response = rest_get(url)
62
- JSON.parse(response.body)
62
+ Azure::Armrest::ResourceGroup.new(response)
63
63
  end
64
64
 
65
65
  # Updates the tags for the given resource group.
@@ -42,7 +42,7 @@ module Azure
42
42
  def list
43
43
  url = build_url
44
44
  response = rest_get(url)
45
- JSON.parse(response.body)["value"]
45
+ JSON.parse(response)["value"].map{ |hash| Azure::Armrest::ResourceProvider.new(hash) }
46
46
  end
47
47
 
48
48
  cache_method(:list, cache_time)
@@ -53,7 +53,7 @@ module Azure
53
53
  def get(namespace)
54
54
  url = build_url(namespace)
55
55
  response = rest_get(url)
56
- JSON.parse(response.body)
56
+ Azure::Armrest::ResourceProvider.new(response)
57
57
  end
58
58
 
59
59
  cache_method(:get, cache_time)
@@ -64,7 +64,7 @@ module Azure
64
64
  def list_geo_locations(namespace)
65
65
  url = build_url(namespace)
66
66
  response = rest_get(url)
67
- JSON.parse(response.body)['resourceTypes'].first['locations']
67
+ JSON.parse(response)['resourceTypes'].first['locations']
68
68
  end
69
69
 
70
70
  cache_method(:list_geo_locations, cache_time)
@@ -75,7 +75,7 @@ module Azure
75
75
  def list_api_versions(namespace)
76
76
  url = build_url(namespace)
77
77
  response = rest_get(url)
78
- JSON.parse(response.body)['resourceTypes'].first['apiVersions']
78
+ JSON.parse(response)['resourceTypes'].first['apiVersions']
79
79
  end
80
80
 
81
81
  cache_method(:list_api_versions, cache_time)
@@ -42,7 +42,7 @@ module Azure
42
42
 
43
43
  response = rest_get(URI.escape(url))
44
44
 
45
- JSON.parse(response.body)["value"]
45
+ JSON.parse(response)["value"].map{ |hash| Azure::Armrest::Resource.new(hash) }
46
46
  end
47
47
 
48
48
  # Move the resources from +source_group+ under +source_subscription+,