Added 'ag list' command with unit tests. Implementation with integration test to follow.

This commit is contained in:
Jeff Mendoza 2013-11-19 13:45:06 -08:00
Родитель 8b6d2a588f
Коммит 2d81381ab2
7 изменённых файлов: 165 добавлений и 6 удалений

62
lib/azure/ag.rb Normal file
Просмотреть файл

@ -0,0 +1,62 @@
#
# Author:: Jeff Mendoza (jeffmendoza@live.com)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
class Azure
class AGs
def initialize(connection)
@connection = connection
end
def load
@ags ||= begin
@ags = {}
response = @connection.query_azure('ags')
response.css('AffinityGroup').each do |ag|
item = AG.new(ag)
@ags[item.name] = item
end
@ags
end
end
def all
load.values
end
def exists?(name)
all.key?(name)
end
def find(name)
load[name]
end
end
end
class Azure
class AG
attr_accessor :name, :label, :description, :location
def initialize(image)
@name = image.at_css('Name').content
@label = image.at_css('Label').content
@description = image.at_css('Description').content if
image.at_css('Description')
@location = image.at_css('Location').content if image.at_css('Location')
end
end
end

Просмотреть файл

@ -24,11 +24,12 @@ require File.expand_path('../role', __FILE__)
require File.expand_path('../disk', __FILE__)
require File.expand_path('../image', __FILE__)
require File.expand_path('../certificate', __FILE__)
require File.expand_path('../ag', __FILE__)
class Azure
class Connection
include AzureAPI
attr_accessor :hosts, :rest, :images, :deploys, :roles, :disks, :storageaccounts, :certificates
attr_accessor :hosts, :rest, :images, :deploys, :roles, :disks, :storageaccounts, :certificates, :ags
def initialize(params={})
@rest = Rest.new(params)
@hosts = Hosts.new(self)
@ -38,6 +39,7 @@ class Azure
@roles = Roles.new(self)
@disks = Disks.new(self)
@certificates = Certificates.new(self)
@ags = AGs.new(self)
end
def query_azure(service_name, verb = 'get', body = '', params = '', wait= true)
Chef::Log.info 'calling ' + verb + ' ' + service_name

Просмотреть файл

@ -0,0 +1,51 @@
#
# Author:: Jeff Mendoza (jeffmendoza@live.com)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require File.expand_path('../azure_base', __FILE__)
class Chef
class Knife
class AzureAgList < Knife
include Knife::AzureBase
deps { require 'highline' }
banner 'knife azure ag list (options)'
def hl
@highline ||= HighLine.new
end
def run
$stdout.sync = true
validate!
cols = %w{Name Location Description}
the_list = cols.map { |col| ui.color(col, :bold) }
connection.ags.all.each do |ag|
cols.each { |attr| the_list << ag.send(attr.downcase).to_s }
end
puts "\n"
puts hl.list(the_list, :uneven_columns_across, cols.size)
end
end
end
end

Просмотреть файл

@ -10,11 +10,12 @@ require 'azure/role'
require 'azure/disk'
require 'azure/utility'
require 'chef/knife/azure_server_list'
require 'chef/knife/azure_server_delete'
require 'chef/knife/azure_server_create'
require 'chef/knife/azure_server_describe'
require 'chef/knife/azure_ag_list'
require 'chef/knife/azure_image_list'
require 'chef/knife/azure_server_create'
require 'chef/knife/azure_server_delete'
require 'chef/knife/azure_server_describe'
require 'chef/knife/azure_server_list'
require 'fileutils'
require "securerandom"

Просмотреть файл

@ -0,0 +1 @@
<AffinityGroups xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><AffinityGroup><Name>agname</Name><Label>YWdsYWJlbA==</Label><Description>agdesc</Description><Location>West US</Location><Capabilities><Capability>PersistentVMRole</Capability><Capability>HighMemory</Capability></Capabilities></AffinityGroup><AffinityGroup><Name>jm-affinity-group</Name><Label>am0tYWZmaW5pdHktZ3JvdXA=</Label><Description/><Location>West US</Location><Capabilities><Capability>PersistentVMRole</Capability><Capability>HighMemory</Capability></Capabilities></AffinityGroup><AffinityGroup><Name>test</Name><Label>dGVzdA==</Label><Description>testdesc</Description><Location>West US</Location><Capabilities><Capability>PersistentVMRole</Capability><Capability>HighMemory</Capability></Capabilities></AffinityGroup></AffinityGroups>

Просмотреть файл

@ -0,0 +1,40 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../unit/query_azure_mock')
describe Chef::Knife::AzureAgList do
include AzureSpecHelper
include QueryAzureMock
before do
@server_instance = Chef::Knife::AzureAgList.new
{
:azure_subscription_id => 'azure_subscription_id',
:azure_mgmt_cert => @cert_file,
:azure_api_host_name => 'preview.core.windows-int.net',
:azure_service_location => 'West Europe',
:azure_source_image =>
'SUSE__SUSE-Linux-Enterprise-Server-11SP2-20120521-en-us-30GB.vhd',
:azure_dns_name => 'service001',
:azure_vm_name => 'vm01',
:azure_storage_account => 'ka001testeurope',
:azure_vm_size => 'Small'
}.each do |key, value|
Chef::Config[:knife][key] = value
end
stub_query_azure(@server_instance.connection)
@server_instance.stub(:items).and_return(:true)
@server_instance.stub(:puts)
end
it 'should display only Name and Location columns.' do
@server_instance.hl.should_receive(:list).with(
['Name', 'Location', 'Description',
'agname', 'West US', 'agdesc',
'jm-affinity-group', 'West US', '',
'test', 'West US', 'testdesc',
],
:uneven_columns_across,
3)
@server_instance.run
end
end

Просмотреть файл

@ -47,7 +47,9 @@ module QueryAzureMock
Chef::Log.info 'calling web service:' + name
if verb == 'get' || verb == nil
retval = ''
if name == 'images'
if name == 'ags'
retval = Nokogiri::XML readFile('list_affinitygroups.xml')
elsif name == 'images'
retval = Nokogiri::XML readFile('list_images.xml')
elsif name == 'disks'
retval = Nokogiri::XML readFile('list_disks.xml')