Verified Commit faa78bba authored by Arif Ali's avatar Arif Ali

Export of the automate datastore, where the work has been done

parent 047b5b64
---
object_type: class
version: 1.0
object:
attributes:
description:
display_name:
name: CSVImport
type:
inherits:
visibility:
owner:
schema:
- field:
aetype: attribute
name: azure_storage_account_name
display_name:
datatype: string
priority: 1
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_resource_group
display_name:
datatype: string
priority: 2
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_share_name
display_name:
datatype: string
priority: 3
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_filename
display_name:
datatype: string
priority: 4
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_client_id
display_name:
datatype: string
priority: 5
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_client_key
display_name:
datatype: password
priority: 6
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_tenant_id
display_name:
datatype: string
priority: 7
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: azure_subscription_id
display_name:
datatype: string
priority: 8
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: cf_category_name
display_name:
datatype: string
priority: 9
owner:
default_value: cf_category_test
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: attribute
name: cf_category_description
display_name:
datatype: string
priority: 10
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: method
name: method0
display_name:
datatype: string
priority: 11
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: method
name: method1
display_name:
datatype: string
priority: 12
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
class FillDropdown
def dropdown_values
csv_content = $evm.get_state_var(:csv_content) || []
# TODO: make unique
values = csv_content.each_with_object({}) { |line, acc| acc[line[:project]] = "#{line[:project]}" }
$evm.object['sort_by'] = 'description'
$evm.object['sort_order'] = 'ascending'
$evm.object['data_type'] = 'string'
$evm.object['required'] = 'true'
$evm.object['values'] = values
$evm.object['default_value'] = values.first
end
end
#
# Main
#
# This automate method is meant to be used to dynamically populate ServiceDialog dropdown with available project names.
# This automate method depends on :csv_content state attribute being set to following format:
# [
# {
# :first_name => String,
# :last_name => String,
# :username => String,
# :email => String,
# :project => String
# },
# ...
# ]
#
if __FILE__ == $PROGRAM_NAME
FillDropdown.new.dropdown_values
end
---
object_type: method
version: 1.0
object:
attributes:
name: fill_dropdown
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs: []
require 'azure/armrest'
require 'csv'
class CsvImporter
attr :conf, :file_conf
def initialize(client_id:, client_key:, tenant_id:, subscription_id:)
@conf = Azure::Armrest::Configuration.new(
:client_id => client_id,
:client_key => client_key,
:tenant_id => tenant_id,
:subscription_id => subscription_id
)
@file_conf = {}
end
def set_file_conf(storage_account_name:, resource_group:, share_name:, filename:)
@file_conf = {
:storage_account_name => storage_account_name,
:resource_group => resource_group,
:share_name => share_name,
:filename => filename
}
self
end
def fetch_file
azure = Azure::Armrest::StorageAccountService.new(@conf)
storage_account = safe_call('Getting storage account') do
azure.get(@file_conf[:storage_account_name], @file_conf[:resource_group])
end
key = safe_call('Getting account key') do
azure.list_account_keys(storage_account.name, @file_conf[:resource_group]).fetch('key1')
end
safe_call('Getting file content') do
storage_account.file_content(@file_conf[:share_name], @file_conf[:filename], key)
end
end
def parse_file(data)
res = []
CSV.parse(data.to_s, :headers => true).each do |line|
res << {
:first_name => line.fields[0],
:last_name => line.fields[1],
:username => line.fields[2],
:email => line.fields[3],
:project => line.fields[4]
}
end
res
end
def safe_call(msg)
$evm.log(:info, msg)
yield
rescue Azure::Armrest::Exception => err
$evm.log(:error, "Error #{msg}: #{err}")
raise err
end
end
#
# Main
#
# This automate method requires Azure credentials that can be passed by either
# (a) calling from within Azure provider so that `$evm.root.attributes['ext_management_system']` is there
# (b) providing azure_client_id, azure_client_key (encrypted), azure_tenant_id, azure_subscription_id as schema attributes
# This automate method requires following file configuration attributes:
# - azure_storage_account_name
# - azure_resource_group
# - azure_share_name
# - azure_filename
# This automate method assumes there is a file named #{azure_filename} present in share #{azure_share_name}.
# This automate method sets :csv_content state attribute in following format:
# [
# {
# :first_name => String,
# :last_name => String,
# :username => String,
# :email => String,
# :project => String
# },
# ...
# ]
#
if __FILE__ == $PROGRAM_NAME
importer = begin
if $evm.object['azure_client_id'].present?
CsvImporter.new(
:client_id => $evm.object['azure_client_id'],
:client_key => $evm.object.decrypt('azure_client_key'),
:tenant_id => $evm.object['azure_tenant_id'],
:subscription_id => $evm.object['azure_subscription_id']
)
elsif (ems = $evm.root.attributes['ext_management_system'])
$evm.log(:info, "Accessing Azure credentials from Azure EMS #{ems.name} (id: #{ems.id})")
CsvImporter.new(
:client_id => ems.authentication_userid,
:client_key => ems.authentication_password,
:tenant_id => ems.uid_ems,
:subscription_id => ems.subscription
)
else
$evm.log(:error, 'Failed to obtain credentials for Azure provider.')
exit(MIQ_STOP)
end
end
importer.set_file_conf(
:storage_account_name => $evm.object['azure_storage_account_name'],
:resource_group => $evm.object['azure_resource_group'],
:share_name => $evm.object['azure_share_name'],
:filename => $evm.object['azure_filename']
)
content = importer.fetch_file
csv_content = importer.parse_file(content)
$evm.log(:info, "CSV content: #{csv_content}")
$evm.set_state_var(:csv_content, csv_content)
end
---
object_type: method
version: 1.0
object:
attributes:
name: import_csv
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs: []
#
# Description: <Method description here>
#
class CSVTagImport
attr :csv_content, :file_conf
def initialize()
@csv_content = $evm.get_state_var(:csv_content) || []
@file_conf = {}
end
def set_file_conf(category_name:,category_description:)
@file_conf = {
:category_name => category_name,
:category_description => category_description
}
self
end
# List all the project tags from the CSV file, and put them into an Array
def get_project_tags()
list_of_tags = []
@csv_content.each do |line|
list_of_tags.push(line[:project])
end
list_of_tags.uniq
end
# Define all the project tags in MS Azure, by grabbing the tags from CSV
def populate_project_tags()
unless $evm.execute('category_exists?', @file_conf[:category_name])
$evm.execute('category_create', :name => @file_conf[:category_name],
:single_value => true,
:description => @file_conf[:category_description])
$evm.log(:info,"Adding tag category: #{@file_conf[:category_name]}")
end
get_project_tags().each do |record|
# The tag name needs to be lowercase, otherwise the function will error out
tag_name = record.downcase
# TODO: Maybe have another column, with a more understandable description. So
# that we can understand what the project code is for
tag_display_name = record.force_encoding(Encoding::UTF_8)
unless $evm.execute('tag_exists?', @file_conf[:category_name], tag_name)
$evm.execute('tag_create', @file_conf[:category_name], :name => tag_name,
:description => tag_display_name)
$evm.log(:info,"Adding tag '#{tag_name}' to category '#{@file_conf[:category_name]}'")
end
end
end
end
if __FILE__ == $PROGRAM_NAME
importer = CSVTagImport.new
importer.set_file_conf(
:category_name => $evm.object['cf_category_name'],
:category_description => $evm.object['cf_category_description']
)
importer.populate_project_tags
end
---
object_type: method
version: 1.0
object:
attributes:
name: populate_tags
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs: []
---
object_type: instance
version: 1.0
object:
attributes:
display_name:
name: fill_dropdown
inherits:
description:
fields:
- azure_storage_account_name:
value: cfmesa
- azure_resource_group:
value: cfmerg
- azure_share_name:
value: testshare
- azure_filename:
value: approver_projects_map.csv
- azure_client_id:
value: 92f5c736-46b2-4b86-8718-0733e8a6accf
- azure_client_key:
value: v2:{qZwqmPkxAbgBGnqggIe9//q/zc9RR/PsR6EAf/aovKrUifG1MjwnmKRV5YF5XC/H}
- azure_tenant_id:
value: e790ea08-42f3-46fa-8819-aca5c465e424
- azure_subscription_id:
value: 6dd30d68-d6b8-4ae3-b011-1eee0fa4d551
- method0:
value: import_csv
- method1:
value: fill_dropdown
---
object_type: instance
version: 1.0
object:
attributes:
display_name:
name: import_csv
inherits:
description:
fields:
- azure_storage_account_name:
value: cfmesa
- azure_resource_group:
value: cfmerg
- azure_share_name:
value: testshare
- azure_filename:
value: approver_projects_map.csv
- azure_client_id:
value: 92f5c736-46b2-4b86-8718-0733e8a6accf
- azure_client_key:
value: v2:{qZwqmPkxAbgBGnqggIe9//q/zc9RR/PsR6EAf/aovKrUifG1MjwnmKRV5YF5XC/H}
- azure_tenant_id:
value: e790ea08-42f3-46fa-8819-aca5c465e424
- azure_subscription_id:
value: 6dd30d68-d6b8-4ae3-b011-1eee0fa4d551
- method0:
value: import_csv
---
object_type: instance
version: 1.0
object:
attributes:
display_name:
name: populate_tags
inherits:
description:
fields:
- azure_storage_account_name:
value: cfmesa
- azure_resource_group:
value: cfmerg
- azure_share_name:
value: testshare
- azure_filename:
value: approver_projects_map.csv
- azure_client_id:
value: 92f5c736-46b2-4b86-8718-0733e8a6accf
- azure_client_key:
value: v2:{qZwqmPkxAbgBGnqggIe9//q/zc9RR/PsR6EAf/aovKrUifG1MjwnmKRV5YF5XC/H}
- azure_tenant_id:
value: e790ea08-42f3-46fa-8819-aca5c465e424
- azure_subscription_id:
value: 6dd30d68-d6b8-4ae3-b011-1eee0fa4d551
- cf_category_name:
value: custom_tag_category
- cf_category_description:
value: Custom Tag Category
- method0:
value: import_csv
- method1:
value: csv_tag_import
---
object_type: namespace
version: 1.0
object:
attributes:
name: Custom
description:
display_name:
priority:
enabled:
---
object_type: class
version: 1.0
object:
attributes:
description:
display_name:
name: Email