Rubocop support for providers/*.rb

* Using single .rubocop.yml

Addresses: blueprint rubocop-for-identity
Change-Id: Ie0f56e57d47a1953011657aef741ba9c5f13c37f
This commit is contained in:
galstrom21 2014-01-10 17:52:14 -06:00
parent c4a0ff26bb
commit 01663915f5
6 changed files with 121 additions and 118 deletions

View File

@ -1,7 +1,30 @@
AllCops:
Includes:
- metadata.rb
- Gemfile
- attributes/**
- providers/**
- resources/**
Excludes:
- recipes/**
- spec/**
# UTF-8 headers not generally in these files
Encoding:
Enabled: false
Exclude:
- metadata.rb
- Gemfile
# ignore long lines
LineLength:
Enabled: false
# Allow small arrays before forcing %w or %W
WordArray:
MinSize: 3
# TODO(galstom21)
# The rescue exception statements in providers/**.rb need to be modified,
# to rescue specific exceptions.
RescueException:
Enabled: false

View File

@ -1,8 +1,5 @@
# Strainerfile
rubocop: bundle exec rubocop $SANDBOX/$COOKBOOK/Gemfile $SANDBOX/$COOKBOOK/metadata.rb --config $SANDBOX/$COOKBOOK/.rubocop.yml
rubocop: bundle exec rubocop $SANDBOX/$COOKBOOK/attributes/*.rb --config $SANDBOX/$COOKBOOK/attributes/.rubocop.yml
rubocop: bundle exec rubocop $SANDBOX/$COOKBOOK/resources/*.rb --config $SANDBOX/$COOKBOOK/resources/.rubocop.yml
# rubocop: bundle exec rubocop $SANDBOX/$COOKBOOK
rubocop: bundle exec rubocop $SANDBOX/$COOKBOOK
knife test: bundle exec knife cookbook test $COOKBOOK
foodcritic: bundle exec foodcritic -f any -t ~FC003 -t ~FC023 $SANDBOX/$COOKBOOK
foodcritic: bundle exec foodcritic -f any -t ~FC003 $SANDBOX/$COOKBOOK
chefspec: bundle exec rspec $SANDBOX/$COOKBOOK/spec

View File

@ -1,7 +0,0 @@
# embedded attributes make for long lines
LineLength:
Enabled: false
# %w or %W have not generally been used in attribute files
WordArray:
Enabled: false

View File

@ -1,3 +1,4 @@
# encoding: UTF-8
#
# Cookbook Name:: openstack-identity
# Provider:: register
@ -25,7 +26,8 @@ include Chef::Mixin::ShellOut
include ::Openstack
private
def generate_creds resource
def generate_creds(resource)
{
'OS_SERVICE_ENDPOINT' => resource.auth_uri,
'OS_SERVICE_TOKEN' => resource.bootstrap_token
@ -33,54 +35,52 @@ def generate_creds resource
end
private
def identity_command resource, cmd, args={}
def identity_command(resource, cmd, args = {})
keystonecmd = ['keystone'] << cmd
args.each { |key, val|
args.each do |key, val|
keystonecmd << "--#{key}" << val.to_s
}
Chef::Log.debug("Running identity command: #{keystonecmd}")
rc = shell_out(keystonecmd, :env => generate_creds(resource))
if rc.exitstatus != 0
raise RuntimeError, "#{rc.stderr} (#{rc.exitstatus})"
end
Chef::Log.debug("Running identity command: #{keystonecmd}")
rc = shell_out(keystonecmd, env: generate_creds(resource))
fail "#{rc.stderr} (#{rc.exitstatus})" if rc.exitstatus != 0
rc.stdout
end
private
def identity_uuid resource, type, key, value, args={}, uuid_field='id'
def identity_uuid(resource, type, key, value, args = {}, uuid_field = 'id') # rubocop: disable ParameterLists
begin
output = identity_command resource, "#{type}-list", args
output = prettytable_to_array(output)
output.each { |obj|
if obj.has_key?(uuid_field) and obj[key] == value
return obj[uuid_field]
end
}
output.each do |obj|
return obj[uuid_field] if obj.key?(uuid_field) && obj[key] == value
end
rescue RuntimeError => e
raise RuntimeError, "Could not lookup uuid for #{type}:#{key}=>#{value}. Error was #{e.message}"
raise "Could not lookup uuid for #{type}:#{key}=>#{value}. Error was #{e.message}"
end
nil
end
action :create_service do
if node["openstack"]["identity"]["catalog"]["backend"] == "templated"
Chef::Log.info("Skipping service creation - templated catalog backend in use.")
if node['openstack']['identity']['catalog']['backend'] == 'templated'
Chef::Log.info('Skipping service creation - templated catalog backend in use.')
new_resource.updated_by_last_action(false)
else
begin
service_uuid = identity_uuid new_resource, "service", "type", new_resource.service_type
service_uuid = identity_uuid new_resource, 'service', 'type', new_resource.service_type
unless service_uuid
identity_command new_resource, "service-create",
{ 'type' => new_resource.service_type,
'name' => new_resource.service_name,
'description' => new_resource.service_description }
Chef::Log.info("Created service '#{new_resource.service_name}'")
new_resource.updated_by_last_action(true)
else
if service_uuid
Chef::Log.info("Service Type '#{new_resource.service_type}' already exists.. Not creating.")
Chef::Log.info("Service UUID: #{service_uuid}")
new_resource.updated_by_last_action(false)
else
identity_command(new_resource, 'service-create',
'type' => new_resource.service_type,
'name' => new_resource.service_name,
'description' => new_resource.service_description)
Chef::Log.info("Created service '#{new_resource.service_name}'")
new_resource.updated_by_last_action(true)
end
rescue Exception => e
Chef::Log.error("Unable to create service '#{new_resource.service_name}'")
@ -91,31 +91,31 @@ action :create_service do
end
action :create_endpoint do
if node["openstack"]["identity"]["catalog"]["backend"] == "templated"
Chef::Log.info("Skipping endpoint creation - templated catalog backend in use.")
if node['openstack']['identity']['catalog']['backend'] == 'templated'
Chef::Log.info('Skipping endpoint creation - templated catalog backend in use.')
new_resource.updated_by_last_action(false)
else
begin
service_uuid = identity_uuid new_resource, "service", "type", new_resource.service_type
service_uuid = identity_uuid new_resource, 'service', 'type', new_resource.service_type
unless service_uuid
Chef::Log.error("Unable to find service type '#{new_resource.service_type}'")
new_resource.updated_by_last_action(false)
next
end
endpoint_uuid = identity_uuid new_resource, "endpoint", "service_id", service_uuid
unless endpoint_uuid
identity_command new_resource, "endpoint-create",
{ 'region' => new_resource.endpoint_region,
'service_id' => service_uuid,
'publicurl' => new_resource.endpoint_publicurl,
'internalurl' => new_resource.endpoint_internalurl,
'adminurl' => new_resource.endpoint_adminurl }
Chef::Log.info("Created endpoint for service type '#{new_resource.service_type}'")
new_resource.updated_by_last_action(true)
else
endpoint_uuid = identity_uuid new_resource, 'endpoint', 'service_id', service_uuid
if endpoint_uuid
Chef::Log.info("Endpoint already exists for Service Type '#{new_resource.service_type}' already exists.. Not creating.")
new_resource.updated_by_last_action(false)
else
identity_command(new_resource, 'endpoint-create',
'region' => new_resource.endpoint_region,
'service_id' => service_uuid,
'publicurl' => new_resource.endpoint_publicurl,
'internalurl' => new_resource.endpoint_internalurl,
'adminurl' => new_resource.endpoint_adminurl)
Chef::Log.info("Created endpoint for service type '#{new_resource.service_type}'")
new_resource.updated_by_last_action(true)
end
rescue Exception => e
Chef::Log.error("Unable to create endpoint for service type '#{new_resource.service_type}'")
@ -127,19 +127,19 @@ end
action :create_tenant do
begin
tenant_uuid = identity_uuid new_resource, "tenant", "name", new_resource.tenant_name
tenant_uuid = identity_uuid new_resource, 'tenant', 'name', new_resource.tenant_name
unless tenant_uuid
identity_command new_resource, "tenant-create",
{ 'name' => new_resource.tenant_name,
'description' => new_resource.tenant_description,
'enabled' => new_resource.tenant_enabled }
Chef::Log.info("Created tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(true)
else
if tenant_uuid
Chef::Log.info("Tenant '#{new_resource.tenant_name}' already exists.. Not creating.")
Chef::Log.info("Tenant UUID: #{tenant_uuid}") if tenant_uuid
new_resource.updated_by_last_action(false)
else
identity_command(new_resource, 'tenant-create',
'name' => new_resource.tenant_name,
'description' => new_resource.tenant_description,
'enabled' => new_resource.tenant_enabled)
Chef::Log.info("Created tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(true)
end
rescue Exception => e
Chef::Log.error("Unable to create tenant '#{new_resource.tenant_name}'")
@ -150,17 +150,17 @@ end
action :create_role do
begin
role_uuid = identity_uuid new_resource, "role", "name", new_resource.role_name
role_uuid = identity_uuid new_resource, 'role', 'name', new_resource.role_name
unless role_uuid
identity_command new_resource, "role-create",
{ 'name' => new_resource.role_name }
Chef::Log.info("Created Role '#{new_resource.role_name}'")
new_resource.updated_by_last_action(true)
else
if role_uuid
Chef::Log.info("Role '#{new_resource.role_name}' already exists.. Not creating.")
Chef::Log.info("Role UUID: #{role_uuid}")
new_resource.updated_by_last_action(false)
else
identity_command(new_resource, 'role-create',
'name' => new_resource.role_name)
Chef::Log.info("Created Role '#{new_resource.role_name}'")
new_resource.updated_by_last_action(true)
end
rescue Exception => e
Chef::Log.error("Unable to create role '#{new_resource.role_name}'")
@ -171,21 +171,20 @@ end
action :create_user do
begin
tenant_uuid = identity_uuid new_resource, "tenant", "name", new_resource.tenant_name
tenant_uuid = identity_uuid new_resource, 'tenant', 'name', new_resource.tenant_name
unless tenant_uuid
Chef::Log.error("Unable to find tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(false)
next
end
output = identity_command new_resource, "user-list", {'tenant-id' => tenant_uuid}
output = identity_command(new_resource, 'user-list',
'tenant-id' => tenant_uuid)
users = prettytable_to_array output
user_found = false
users.each { |user|
if user['name'] == new_resource.user_name
user_found = true
end
}
users.each do |user|
user_found = true if user['name'] == new_resource.user_name
end
if user_found
Chef::Log.info("User '#{new_resource.user_name}' already exists for tenant '#{new_resource.tenant_name}'")
@ -193,11 +192,11 @@ action :create_user do
next
end
identity_command new_resource, "user-create",
{ 'name' => new_resource.user_name,
'tenant-id' => tenant_uuid,
'pass' => new_resource.user_pass,
'enabled' => new_resource.user_enabled }
identity_command(new_resource, 'user-create',
'name' => new_resource.user_name,
'tenant-id' => tenant_uuid,
'pass' => new_resource.user_pass,
'enabled' => new_resource.user_enabled)
Chef::Log.info("Created user '#{new_resource.user_name}' for tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(true)
rescue Exception => e
@ -209,40 +208,41 @@ end
action :grant_role do
begin
tenant_uuid = identity_uuid new_resource, "tenant", "name", new_resource.tenant_name
tenant_uuid = identity_uuid new_resource, 'tenant', 'name', new_resource.tenant_name
unless tenant_uuid
Chef::Log.error("Unable to find tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(false)
next
end
user_uuid = identity_uuid new_resource, "user", "name", new_resource.user_name
user_uuid = identity_uuid new_resource, 'user', 'name', new_resource.user_name
unless tenant_uuid
Chef::Log.error("Unable to find user '#{new_resource.user_name}'")
new_resource.updated_by_last_action(false)
next
end
role_uuid = identity_uuid new_resource, "role", "name", new_resource.role_name
role_uuid = identity_uuid new_resource, 'role', 'name', new_resource.role_name
unless tenant_uuid
Chef::Log.error("Unable to find role '#{new_resource.role_name}'")
new_resource.updated_by_last_action(false)
next
end
assigned_role_uuid = identity_uuid new_resource, "user-role", "name", new_resource.role_name,
{ 'tenant-id' => tenant_uuid,
'user-id' => user_uuid }
unless role_uuid == assigned_role_uuid
identity_command new_resource, "user-role-add",
{ 'tenant-id' => tenant_uuid,
'role-id' => role_uuid,
'user-id' => user_uuid }
Chef::Log.info("Granted Role '#{new_resource.role_name}' to User '#{new_resource.user_name}' in Tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(true)
else
assigned_role_uuid = identity_uuid(new_resource, 'user-role', 'name',
new_resource.role_name,
'tenant-id' => tenant_uuid,
'user-id' => user_uuid)
if role_uuid == assigned_role_uuid
Chef::Log.info("Role '#{new_resource.role_name}' already granted to User '#{new_resource.user_name}' in Tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(false)
else
identity_command(new_resource, 'user-role-add',
'tenant-id' => tenant_uuid,
'role-id' => role_uuid,
'user-id' => user_uuid)
Chef::Log.info("Granted Role '#{new_resource.role_name}' to User '#{new_resource.user_name}' in Tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(true)
end
rescue Exception => e
Chef::Log.error("Unable to grant role '#{new_resource.role_name}' to user '#{new_resource.user_name}'")
@ -253,14 +253,16 @@ end
action :create_ec2_credentials do
begin
tenant_uuid = identity_uuid new_resource, "tenant", "name", new_resource.tenant_name
tenant_uuid = identity_uuid new_resource, 'tenant', 'name', new_resource.tenant_name
unless tenant_uuid
Chef::Log.error("Unable to find tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(false)
next
end
user_uuid = identity_uuid new_resource, "user", "name", new_resource.user_name, {'tenant-id' => tenant_uuid}
user_uuid = identity_uuid(new_resource, 'user', 'name',
new_resource.user_name,
'tenant-id' => tenant_uuid)
unless tenant_uuid
Chef::Log.error("Unable to find user '#{new_resource.user_name}'")
new_resource.updated_by_last_action(false)
@ -268,11 +270,14 @@ action :create_ec2_credentials do
end
# this is not really a uuid, but this will work nonetheless
access = identity_uuid new_resource, "ec2-credentials", "tenant", new_resource.tenant_name, {'user-id' => user_uuid}, "access"
unless access
output = identity_command new_resource, "ec2-credentials-create",
{ 'user-id' => user_uuid,
'tenant-id' => tenant_uuid }
access = identity_uuid new_resource, 'ec2-credentials', 'tenant', new_resource.tenant_name, { 'user-id' => user_uuid }, 'access'
if access
Chef::Log.info("EC2 credentials already exist for '#{new_resource.user_name}' in tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(false)
else
output = identity_command(new_resource, 'ec2-credentials-create',
'user-id' => user_uuid,
'tenant-id' => tenant_uuid)
Chef::Log.info("Created EC2 Credentials for User '#{new_resource.user_name}' in Tenant '#{new_resource.tenant_name}'")
data = prettytable_to_array(output)
@ -286,9 +291,6 @@ action :create_ec2_credentials do
node.save unless Chef::Config[:solo]
new_resource.updated_by_last_action(true)
end
else
Chef::Log.info("EC2 credentials already exist for '#{new_resource.user_name}' in tenant '#{new_resource.tenant_name}'")
new_resource.updated_by_last_action(false)
end
rescue Exception => e
Chef::Log.error("Unable to create EC2 Credentials for User '#{new_resource.user_name}' in Tenant '#{new_resource.tenant_name}'")
@ -296,6 +298,3 @@ action :create_ec2_credentials do
new_resource.updated_by_last_action(false)
end
end

View File

@ -1,9 +0,0 @@
# This configuration was generated by `rubocop --auto-gen-config`.
# The point is for the user to remove these configuration records
# one by one as the offences are removed from the code base.
LineLength:
Enabled: false
WordArray:
Enabled: false

View File

@ -33,7 +33,7 @@ attribute :auth_uri, kind_of: String
attribute :bootstrap_token, kind_of: String
# Used by both :create_service and :create_endpoint
attribute :service_type, kind_of: String, equal_to: ['image', 'identity', 'compute', 'storage', 'ec2', 'volume', 'object-store', 'metering', 'network', 'orchestration', 'cloudformation']
attribute :service_type, kind_of: String, equal_to: %w{image identity compute storage ec2 volume object-store metering network orchestration cloudformation}
# :create_service specific attributes
attribute :service_name, kind_of: String