Skip to content
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
7 changes: 7 additions & 0 deletions lib/fog/azurerm/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class AzureRM < Fog::Service
request :create_image
request :delete_image
request :get_image
request :create_or_update_snapshot
request :list_snapshots_by_rg
request :list_snapshots_in_subscription
request :get_snapshot
request :delete_snapshot

model_path 'fog/azurerm/models/compute'
model :availability_set
Expand All @@ -54,6 +59,8 @@ class AzureRM < Fog::Service
collection :virtual_machine_extensions
model :managed_disk
collection :managed_disks
model :snapshot
collection :snapshots
model :data_disk
model :creation_data
model :disk_create_option
Expand Down
1 change: 1 addition & 0 deletions lib/fog/azurerm/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
RESOURCE_NAME = 8
ENDPOINT_PREFIX = 'Microsoft.Network/trafficManagerProfiles'.freeze
DISK_PREFIX = 'Microsoft.Compute/disks'.freeze
SNAPSHOT_PREFIX = 'Microsoft.Compute/snapshots'.freeze
AZURE_ENDPOINTS = 'azureEndpoints'.freeze
EXTERNAL_ENDPOINTS = 'externalEndpoints'.freeze
NESTED_ENDPOINTS = 'nestedEndpoints'.freeze
Expand Down
61 changes: 51 additions & 10 deletions lib/fog/azurerm/docs/compute.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This document explains how to get started using Azure Compute Service with Fog. With this gem you can create, update, list or delete availability sets and virtual machines.

## Usage
## Usage

First of all, you need to require the Fog library by executing:

Expand Down Expand Up @@ -50,7 +50,7 @@ fog_compute_service.servers.create(
username: '<Username>',
disable_password_authentication: <True/False>,
network_interface_card_ids: ['/subscriptions/<Subscription Id>/resourceGroups/<Resource Group Name>/providers/Microsoft.Network/networkInterfaces/<Network Interface Id>'],
publisher: '<Publisher Name>', # Not required if custom image is being used
publisher: '<Publisher Name>', # Not required if custom image is being used
offer: '<Offer Name>', # Not required if custom image is being used
sku: '<SKU Name>', # Not required if custom image is being used
version: '<Version>', # Not required if custom image is being used
Expand Down Expand Up @@ -116,7 +116,7 @@ fog_compute_service.servers.create(
disable_password_authentication: <True/False>,
network_interface_card_ids: ['/subscriptions/<Subscription Id>/resourceGroups/<Resource Group Name>/providers/Microsoft.Network/networkInterfaces/<Network Interface Id>'],
publisher: '<Publisher Name>', # Not required if custom image is being used
offer: '<Offer Name>', # Not required if custom image is being used
offer: '<Offer Name>', # Not required if custom image is being used
sku: '<SKU Name>', # Not required if custom image is being used
version: '<Version>', # Not required if custom image is being used
platform: '<OS Type>',
Expand All @@ -142,11 +142,11 @@ async_response = fog_compute_service.servers.create_async(
username: '<Username for VM>',
disable_password_authentication: <True/False>,
network_interface_card_ids: ['/subscriptions/<Subscription Id>/resourceGroups/<Resource Group Name>/providers/Microsoft.Network/networkInterfaces/<Network Interface Id>'],
publisher: '<Publisher Name>', # Not required if custom image is being used
publisher: '<Publisher Name>', # Not required if custom image is being used
offer: '<Offer Name>', # Not required if custom image is being used
sku: '<SKU Name>', # Not required if custom image is being used
version: '<Version>' , # Not required if custom image is being used
platform: '<OS Type>',
platform: '<OS Type>',
availability_set_id: '<Availability Set Id>', # [Optional]
password: '<Password>', # [Optional], if 'platform' partameter is 'Linux'.
vhd_path: '<Path of VHD>', # [Optional], if you want to create the VM from a custom image.
Expand All @@ -169,7 +169,7 @@ An example of handling async response is given below:
```ruby
while 1
puts async_response.state

if async_response.pending?
sleep(2)
end
Expand All @@ -185,7 +185,7 @@ while 1
end
end
```

For more information about custom_data, see link: https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx

## List Servers
Expand Down Expand Up @@ -215,7 +215,7 @@ puts "#{server.name}"

Check the status of a Server

```ruby
```ruby
status = fog_compute_service
.servers
.get('<Resource Group Name>', '<Server Name>')
Expand Down Expand Up @@ -301,7 +301,7 @@ List managed disks in a resource group

```ruby
managed_disks = fog_compute_service.managed_disks(resource_group: '<Resource Group Name>')
mnaged_disks.each do |disk|
managed_disks.each do |disk|
puts "#{disk.name}"
puts "#{disk.location}"
end
Expand Down Expand Up @@ -361,6 +361,47 @@ Get an managed disk object from the get method and then destroy that managed dis
managed_disk.destroy
```

## List Snapshots in a Resource Group

List Snapshots in a Resource Group

```ruby
snapshots = fog_compute_service.snapshots(resource_group: '<Resource Group Name>')
snapshots.each do |snap|
puts "#{snap.name}"
puts "#{snap.location}"
end
```

## List Snapshots in a Subscription

List Snapshots in a subscription

```ruby
snapshots = fog_compute_service.snapshots
snapshots.each do |snap|
puts "#{snap.name}"
puts "#{snap.location}
end
```

## Get one Snapshot in a Resource Group

get one Snapshot in a Resource Group

```ruby
snap = fog_compute_service.snapshots.get('<Resource Group Name>', 'snapshot-name')
puts "#{snap.name}"
puts "#{snap.location}
```

## Destroy a Snapshot
Destroy a Snapshot

```ruby
snapshot.destroy
```

## Check Availability Set Existence

```ruby
Expand All @@ -381,7 +422,7 @@ fog_compute_service.availability_sets.create(
use_managed_disk: true # [Optional] Possible values true or false
)
```
## List Availability Sets
## List Availability Sets

List availability sets in a resource group

Expand Down
95 changes: 95 additions & 0 deletions lib/fog/azurerm/models/compute/snapshot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Fog
module Compute
class AzureRM
# This class is giving implementation of create/save and
# delete/destroy for snapshot.
class Snapshot < Fog::Model
attribute :id
identity :name
attribute :location
attribute :resource_group_name
attribute :account_type
attribute :disk_size_gb
attribute :tags
attribute :time_created
attribute :creation_data
attribute :encryption_settings
attribute :type
attribute :owner_id
attribute :provisioning_state
attribute :os_type

def self.parse(snapshot)
snap = get_hash_from_object(snapshot)

snap['creation_data'] = parse_creation_data(snapshot.creation_data) unless snapshot.creation_data.nil?

snap['encryption_settings'] = parse_encryption_settings_object(snapshot.encryption_settings) unless snapshot.encryption_settings.nil?

snap['resource_group_name'] = get_resource_group_from_id(snapshot.id)

snap
end

def destroy(async = false)
response = service.delete_snapshot(resource_group_name, name, async)
async ? create_fog_async_response(response) : response
end

def creation_data=(new_creation_data)
attributes[:creation_data] =
if new_creation_data.is_a?(Hash)
creation_data = Fog::Compute::AzureRM::CreationData.new
creation_data.merge_attributes(new_creation_data)
else
new_creation_data
end
end

def self.parse_creation_data(azure_sdk_creation_data)
creation_data = Fog::Compute::AzureRM::CreationData.new
creation_data.merge_attributes(Fog::Compute::AzureRM::CreationData.parse(azure_sdk_creation_data))
end

def self.parse_encryption_settings_object(azure_sdk_encryption_settings)
encryption_settings = Fog::Compute::AzureRM::EncryptionSettings.new
encryption_settings.merge_attributes(Fog::Compute::AzureRM::EncryptionSettings.parse(azure_sdk_encryption_settings))
end

private_class_method :parse_creation_data, :parse_encryption_settings_object

def save
requires :name, :location, :resource_group_name, :creation_data
validate_creation_data_params(creation_data)

snapshot = service.create_or_update_snapshot(snapshot_params)
merge_attributes(Fog::Compute::AzureRM::Snapshot.parse(snapshot))
end

private

def validate_creation_data_params(creation_data)
if !creation_data || creation_data.create_option.nil?
raise(ArgumentError, 'creation_data.create_option is required for this operation')
end
end

def snapshot_params
{
name: name,
location: location,
resource_group_name: resource_group_name,
tags: tags,
creation_data: creation_data.attributes,
encryption_settings: encryption_settings
}
end

def create_fog_async_response(response, delete_extra_resource = false)
snapshot = Fog::Compute::AzureRM::Snapshot.new(service: service)
Fog::AzureRM::AsyncResponse.new(snapshot, response, delete_extra_resource)
end
end
end
end
end
30 changes: 30 additions & 0 deletions lib/fog/azurerm/models/compute/snapshots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Fog
module Compute
class AzureRM
# This class is giving implementation of all/list, get and
# check existence for snapshots of managed disk.
class Snapshots < Fog::Collection
model Fog::Compute::AzureRM::Snapshot
attribute :resource_group

def all
snapshots = if resource_group.nil?
service.list_snapshots_in_subscription
else
requires :resource_group
service.list_snapshots_by_rg(resource_group)
end
snapshots = snapshots.map { |snapshot| Fog::Compute::AzureRM::Snapshot.parse snapshot }

load(snapshots)
end

def get(resource_group_name, snap_name)
snapshot = service.get_snapshot(resource_group_name, snap_name)
snapshot_fog = Fog::Compute::AzureRM::Snapshot.new(service: service)
snapshot_fog.merge_attributes(Fog::Compute::AzureRM::Snapshot.parse(snapshot))
end
end
end
end
end
Loading