Merge "Adding nova creating and deleting for client"

This commit is contained in:
Jenkins 2015-04-21 20:42:31 +00:00 committed by Gerrit Code Review
commit 60d6a29184
12 changed files with 255 additions and 15 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ berks-cookbooks/
Berksfile.lock
Vagrantfile
Gemfile.lock
local-mode-cache
nodes/

View File

@ -7,11 +7,19 @@ provisioner:
platforms:
- name: ubuntu-14.04
- name: ubuntu-12.04
- name: centos-6.6
- name: centos-7.0
suites:
- name: default
run_list:
- recipe[openstack::default]
- recipe[openstack::_example_swift]
- recipe[openstack_client::default]
attributes:
- name: swift
run_list:
- recipe[openstack_client::_example_swift]
attributes:
- name: nova
run_list:
- recipe[openstack_client::_example_nova]
attributes:

View File

@ -8,6 +8,8 @@ gem 'chefspec', '~> 4.0.0'
gem 'rspec', '~> 3.0.0'
gem 'foodcritic', '~> 4.0'
gem 'rubocop', '~> 0.29.1'
gem 'chef-sugar'
gem 'fog'
gem 'rake', '~> 10.0'
gem 'test-kitchen'

16
libraries/matchers.rb Normal file
View File

@ -0,0 +1,16 @@
# encoding: UTF-8
if defined?(ChefSpec)
def create_openstack_client_nova_server(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(
:openstack_client_nova_server,
:create,
resource_name)
end
def delete_openstack_client_nova_server(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(
:openstack_client_nova_server,
:delete,
resource_name)
end
end

36
libraries/nova.rb Normal file
View File

@ -0,0 +1,36 @@
#
# Cookbook Name:: openstack-client
# Library:: nova
# Author:: JJ Asghar
#
# 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.
#
module Openstack
module Client
# this is required to shut up rubocop
module Nova
include Openstack::Client
def nova # rubocop:disable all
@nova ||= Fog::Compute.new(provider: 'openstack',
openstack_username: new_resource.openstack_username,
openstack_api_key: new_resource.openstack_password,
openstack_auth_url: new_resource.openstack_auth_url,
openstack_tenant: new_resource.openstack_tenant,
openstack_region: new_resource.openstack_region
) # rubocop:disable all
end
end
end
end

View File

@ -1,5 +1,5 @@
# Encoding: utf-8
name 'openstack-client'
name 'openstack_client'
maintainer 'openstack-chef'
maintainer_email 'opscode-chef-openstack@googlegroups.com'
license 'Apache2'
@ -8,7 +8,9 @@ long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '11.0.0'
supports 'ubuntu', '>= 14.04'
supports 'centos', '>= 6.5'
depends 'apt', '~> 2.6.1'
depends 'build-essential', '~> 2.1.3'
depends 'xml', '~> 1.2.13'
depends 'yum', '~> 3.5.4'

52
providers/nova_server.rb Normal file
View File

@ -0,0 +1,52 @@
#
# Cookbook Name:: openstack-client
# Provider:: nova_server
# Author:: JJ Asghar
#
# 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 'tempfile'
require 'chef/digester'
include Openstack::Client::Nova
def whyrun_supported?
true
end
action :create do
require 'fog'
name = new_resource.name
image = new_resource.image
flavor = new_resource.flavor
ruby_block 'create a server' do
block do
nova.create_server(name, image, flavor)
end
end
new_resource.updated_by_last_action(true)
end
action :delete do
require 'fog'
id = new_resource.id
ruby_block 'delete a server' do
block do
nova.delete_server(id)
end
end
new_resource.updated_by_last_action(true)
end

40
recipes/_example_nova.rb Normal file
View File

@ -0,0 +1,40 @@
#
# Cookbook Name:: openstack-client
# Provider:: _example_nova
# Author:: JJ Asghar
#
# 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.
#
include_recipe 'openstack_client::default'
openstack_client_nova_server 'test1' do
openstack_username 'USERNAME'
openstack_password 'PASSWORD'
openstack_auth_url 'https://mybackendopenstack:35357/v2.0/tokens'
openstack_tenant 'Project'
openstack_region 'region-a'
image 'bec3cab5-4722-40b9-a78a-3489218e22fe'
flavor '101'
action :create
end
openstack_client_nova_server 'test' do
openstack_username 'USERNAME'
openstack_password 'PASSWORD'
openstack_auth_url 'https://mybackendopenstack:35357/v2.0/tokens'
openstack_tenant 'Project'
openstack_region 'region-a'
id 'c6ab5354-baa4-4ea3-af27-ef3eb874472e'
action :delete
end

View File

@ -8,26 +8,25 @@
# Required to install fog
if platform_family?('debian')
case node['platform_family']
when 'debian'
node.override['apt']['compile_time_update'] = true
include_recipe 'apt'
node.set['build-essential']['compile_time'] = true
include_recipe 'build-essential'
when 'redhat'
include_recipe 'yum'
end
node.set['build-essential']['compile_time'] = true
include_recipe 'build-essential'
include_recipe 'xml::ruby'
chef_gem 'nokogiri' do
version node['openstack-client']['nokogiri_version']
action :upgrade
end
include_recipe 'xml::ruby'
chef_gem 'fog' do
version node['openstack-client']['fog_version']
action :upgrade
end
# Load fog for the cloud_monitoring library
# https://sethvargo.com/using-gems-with-chef/
require 'fog'

38
resources/nova_server.rb Normal file
View File

@ -0,0 +1,38 @@
#
# Cookbook Name:: openstack-client
# Resource:: nova_server
# Author:: JJ Asghar
#
# 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.
#
actions :create, :delete
default_action :create
BOOLEAN = [TrueClass, FalseClass]
attribute :openstack_username, kind_of: String, required: true
attribute :openstack_password, kind_of: String, required: true
attribute :openstack_tenant, kind_of: String, required: true
attribute :openstack_region, kind_of: String, default: 'RegionOne'
attribute :openstack_auth_url, kind_of: String, required: true
attribute :name, kind_of: String, name_attribute: true
attribute :id, kind_of: String, name_attribute: true
attribute :image, kind_of: String, required: true
attribute :flavor, kind_of: String
def initialize(*args)
super
@action = :create
end

View File

@ -0,0 +1,45 @@
# Encoding: utf-8
require_relative 'spec_helper'
describe 'openstack_client::_example_nova' do
let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) }
let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) }
it 'spins up a test machine' do
expect(chef_run).to create_openstack_client_nova_server(
'test1'
).with(
action: [:create]
)
end
end
describe 'openstack_client::_example_nova' do
let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) }
let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) }
it 'deletes a test machine' do
expect(chef_run).to delete_openstack_client_nova_server(
'test'
).with(
action: [:delete]
)
end
end
describe 'openstack_client::_example_nova' do
let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) }
let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) }
it 'install the nokogiri gem' do
expect(chef_run).to install_chef_gem('nokogiri')
end
it 'install the fog gem' do
expect(chef_run).to upgrade_chef_gem('fog')
end
end

View File

@ -4,8 +4,8 @@ require 'chefspec'
require 'chefspec/berkshelf'
require 'chef/application'
::LOG_LEVEL = :fatal
::UBUNTU_OPTS = {
LOG_LEVEL = :fatal
UBUNTU_OPTS = {
platform: 'ubuntu',
version: '14.04',
log_level: ::LOG_LEVEL