Skip to content

Commit

Permalink
Merge pull request #14 from Ladas/using_api_for_service_and_template_…
Browse files Browse the repository at this point in the history
…creation_and_deletion

Using api for service and template creation and deletion
  • Loading branch information
Ladas authored Sep 22, 2016
2 parents cfea078 + 883f2c1 commit 36bbb82
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,25 @@ def get_networks_template(network_service, parent_service)
end

vnf_networks_template_name = "#{parent_service.name} networks #{parent_service.id}"

template = $evm.vmdb('orchestration_template_hot').create(
:name => vnf_networks_template_name,
:orderable => true,
:content => YAML.dump(template_content))
template

resource = {:name => vnf_networks_template_name,
:type => "OrchestrationTemplateHot",
:orderable => true,
:content => YAML.dump(template_content)}

url = "http://localhost:3000/api/orchestration_templates"
options = {:method => :post,
:url => url,
:verify_ssl => false,
:payload => {"action" => "create",
"resource" => resource}.to_json,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
$evm.log("info", "Creating HOT template #{options}")

body = JSON.parse(RestClient::Request.execute(options))

$evm.vmdb('orchestration_template_hot', body["results"].first["id"])
end

def deploy_networks(network_service, parent_service)
Expand Down Expand Up @@ -93,22 +106,36 @@ def deploy_networks(network_service, parent_service)
end

def deploy_networks_stack(orchestration_manager, parent_service, template)
resource = {:name => "#{parent_service.name} networks",
:type => "ServiceOrchestration",
:orchestration_template => {:id => template.id},
:orchestration_manager => {:id => orchestration_manager.id},
:parent_service => {:id => parent_service.id},
:stack_name => "#{parent_service.name}_#{$evm.root['service_template_provision_task_id']}_networks",
:stack_options => {:attributes => {}},
:display => true}

url = "http://localhost:3000/api/services"
options = {:method => :post,
:url => url,
:verify_ssl => false,
:payload => {"action" => "create",
"resource" => resource}.to_json,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
$evm.log("info", "Creating HOT service #{options}")

orchestration_service = $evm.vmdb('ServiceOrchestration').create(
:name => "#{parent_service.name} networks")
body = JSON.parse(RestClient::Request.execute(options))

orchestration_service.stack_name = "#{parent_service.name}_#{$evm.root['service_template_provision_task_id']}_networks"
orchestration_service.orchestration_template = template
orchestration_service.orchestration_manager = orchestration_manager
orchestration_service.stack_options = {:attributes => {}}
orchestration_service.display = true
orchestration_service.parent_service = parent_service
orchestration_service.deploy_orchestration_stack
orchestration_service = $evm.vmdb('service', body["results"].first["id"])
orchestration_service.deploy_orchestration_stack

orchestration_service
end

begin
require 'rest-client'

nsd = $evm.get_state_var(:nsd)
$evm.log("info", "Listing nsd #{nsd}")
$evm.log("info", "Listing Root Object Attributes:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,25 @@ def create_template(name,nsd_properties,nsd_requirements)

$evm.log(:info ,"nic array==> #{nic}")
template_content["Resources"].merge!(instance("Ec2Instance", nic, key_name, image.ems_ref,availability_zone,instance_type))
$evm.vmdb('orchestration_template_cfn').create(
:name => name,
:orderable => true,
:content => JSON.pretty_generate(template_content))

resource = {:name => name,
:type => "OrchestrationTemplateCfn",
:orderable => true,
:content => JSON.pretty_generate(template_content)}

url = "http://localhost:3000/api/orchestration_templates"
options = {:method => :post,
:url => url,
:verify_ssl => false,
:payload => {"action" => "create",
"resource" => resource}.to_json,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
$evm.log("info", "Creating CFN template #{options}")

body = JSON.parse(RestClient::Request.execute(options))

$evm.vmdb('orchestration_template_cfn', body["results"].first["id"])
end

def deploy_amazon_stack(orchestration_manager, parent_service, vnf_service)
Expand All @@ -182,17 +197,29 @@ def deploy_amazon_stack(orchestration_manager, parent_service, vnf_service)
name = "#{parent_service.name} #{vnf_service.name} #{parent_service.id}"
template = create_template(name,nsd_properties,nsd_requirements)

$evm.log("info", "Deploying CFN template #{name}")
orchestration_service = $evm.vmdb('ServiceOrchestration').create(
:name => "#{parent_service.name} #{vnf_service.name}")
resource = {:name => "#{parent_service.name} #{vnf_service.name}",
:type => "ServiceOrchestration",
:orchestration_template => {:id => template.id},
:orchestration_manager => {:id => orchestration_manager.id},
:parent_service => {:id => parent_service.id},
:stack_name => name.gsub("\s", "-").gsub("_", "-"),
:stack_options => {},
:display => true}

url = "http://localhost:3000/api/services"
options = {:method => :post,
:url => url,
:verify_ssl => false,
:payload => {"action" => "create",
"resource" => resource}.to_json,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
$evm.log("info", "Creating CFN service #{options}")

body = JSON.parse(RestClient::Request.execute(options))

orchestration_service = $evm.vmdb('service', body["results"].first["id"])
orchestration_service.custom_set('properties', nsd_properties.to_json)
orchestration_service.stack_name = name.gsub("\s", "-").gsub("_", "-")
orchestration_service.orchestration_template = template
orchestration_service.orchestration_manager = orchestration_manager
orchestration_service.stack_options = {}
orchestration_service.display = true
orchestration_service.parent_service = parent_service
orchestration_service.deploy_orchestration_stack
end

Expand Down Expand Up @@ -220,6 +247,8 @@ def ethx_config(ethx)
end

begin
require 'rest-client'

nsd = $evm.get_state_var(:nsd)
$evm.log("info", "Listing nsd #{nsd}")
$evm.log("info", "Listing Root Object Attributes:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def get_template_contents(network_service)
end

def get_template(orchestration_manager, network_service, parent_service, vnf_service)
templates = {}
# Load our general VNFDs per type
template_contents = get_template_contents(network_service)

Expand Down Expand Up @@ -108,12 +107,27 @@ def get_template(orchestration_manager, network_service, parent_service, vnf_ser

template_content['topology_template']['node_templates'].merge!(virtual_links)
unused_cps.each { |x| template_content['topology_template']['node_templates'].delete(x) }

$evm.vmdb('orchestration_template_vnfd').create(
:name => vnf_template_name,
:orderable => true,
:ems_id => orchestration_manager.id,
:content => YAML.dump(template_content))

resource = {:name => vnf_template_name,
:type => "OrchestrationTemplateVnfd",
:orderable => true,
:remote_proxy => true,
:ems_id => orchestration_manager.id,
:content => YAML.dump(template_content)}

url = "http://localhost:3000/api/orchestration_templates"
options = {:method => :post,
:url => url,
:verify_ssl => false,
:payload => {"action" => "create",
"resource" => resource}.to_json,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
$evm.log("info", "Creating VNFd template #{options}")

body = JSON.parse(RestClient::Request.execute(options))

$evm.vmdb('orchestration_template_vnfd', body["results"].first["id"])
end

def deploy_vnfs(network_service, parent_service)
Expand All @@ -135,21 +149,36 @@ def deploy_vnf_stack(orchestration_manager, network_service, parent_service, vnf
params['type'] = vnf_service.custom_get('type')

params = params.to_json

orchestration_service = $evm.vmdb('ServiceOrchestration').create(
:name => "#{parent_service.name} #{vnf_service.name}")


resource = {:name => "#{parent_service.name} #{vnf_service.name}",
:type => "ServiceOrchestration",
:orchestration_template => {:id => template.id},
:orchestration_manager => {:id => orchestration_manager.id},
:parent_service => {:id => parent_service.id},
:stack_name => "#{parent_service.name} #{vnf_service.name} #{parent_service.id}",
:stack_options => {:attributes => {:param_values => params}},
:display => true}

url = "http://localhost:3000/api/services"
options = {:method => :post,
:url => url,
:verify_ssl => false,
:payload => {"action" => "create",
"resource" => resource}.to_json,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
$evm.log("info", "Creating Vnf service #{options}")

body = JSON.parse(RestClient::Request.execute(options))

orchestration_service = $evm.vmdb('service', body["results"].first["id"])
orchestration_service.custom_set('properties', params)
orchestration_service.stack_name = "#{parent_service.name} #{vnf_service.name} #{parent_service.id}"
orchestration_service.orchestration_template = template
orchestration_service.orchestration_manager = orchestration_manager
orchestration_service.stack_options = {:attributes => {:param_values => params}}
orchestration_service.display = true
orchestration_service.parent_service = parent_service
orchestration_service.deploy_orchestration_stack
end

begin
require 'rest-client'

nsd = $evm.get_state_var(:nsd)
$evm.log("info", "Listing nsd #{nsd}")
$evm.log("info", "Listing Root Object Attributes:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ def retire_vnfs(network_service)
# This a VNF service

stack = $evm.vmdb('ManageIQ_Providers_Openstack_CloudManager_Vnf').find_by_name("#{vnf_service.name} #{network_service.id}")

if stack
status, reason = vnf_service.orchestration_stack.normalized_live_status

if stack && status != 'not_exist'
# Tacker stack
if vnf_service.orchestration_stack_status[0] == 'create_complete'
stack.raw_delete_stack()
$evm.log(:info, "Retiring #{vnf_service.name}")
vnf_service.retire_now()
found_stack = true
elsif vnf_service.orchestration_stack_status[0] == 'transient'
found_stack = true
found_stack = true
if vnf_service.orchestration_stack_status[0] == 'create_complete' || vnf_service.orchestration_stack_status[0] == 'update_complete'
$evm.log(:info, "Deleting VNF stack#{vnf_service.name}")
delete_stack(stack)
end
else
# Could be a Tacker template remaining...
# Could be a Tacker template remaining...
$evm.log(:info, "Properties '#{vnf_service.custom_get('properties')}' VNFD orchestration template for deletion")
type = JSON.parse(vnf_service.custom_get('properties') || '{}').try(:[], 'type') || ""
$evm.log(:info, "Finding '#{vnf_service.name} #{type} #{network_service.id}' VNFD orchestration template for deletion")
template = $evm.vmdb('orchestration_template_vnfd').find_by_name("#{vnf_service.name} #{type} #{network_service.id}")

if template
Expand All @@ -29,9 +29,15 @@ def retire_vnfs(network_service)
next
end

$evm.log(:info, "Deleting #{vnf_service.name} VNFD orchestration template")
temp_vnfd = $evm.vmdb('orchestration_template_vnfd')
temp_vnfd.destroy(template.id)
$evm.log(:info, "Deleting #{vnf_service.name} VNFD #{template.id} orchestration template")

url = "http://localhost:3000/api/orchestration_templates/#{template.id}"
options = {:method => :delete,
:url => url,
:verify_ssl => false,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
RestClient::Request.execute(options)
end

# ...but also could be an AWS (CFN) stack
Expand All @@ -40,7 +46,7 @@ def retire_vnfs(network_service)

if stack != nil
if vnf_service.orchestration_stack_status[0] == 'create_complete'
stack.raw_delete_stack()
delete_stack(stack)
$evm.log(:info, "Retiring #{vnf_service.name}")
vnf_service.retire_now()
found_stack = true
Expand All @@ -51,11 +57,18 @@ def retire_vnfs(network_service)
if vnf_service.respond_to?(:orchestration_stack_status) && (vnf_service.orchestration_stack_status[0] == 'create_complete' or vnf_service.orchestration_stack_status[0] == 'transient')
found_stack = true
else
$evm.log(:info, "Finding '#{vnf_service.name} #{network_service.id}' CFN orchestration template for deletion")
template = $evm.vmdb('orchestration_template_cfn').find_by_name("#{vnf_service.name} #{network_service.id}")

if template != nil
$evm.log(:info, "Deleting #{vnf_service.name} CFN orchestration template")
$evm.vmdb('orchestration_template_cfn').destroy(template.id)
url = "http://localhost:3000/api/orchestration_templates/#{template.id}"
options = {:method => :delete,
:url => url,
:verify_ssl => false,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
RestClient::Request.execute(options)
end
end
end
Expand All @@ -66,7 +79,13 @@ def retire_vnfs(network_service)

if template != nil
$evm.log(:info, "Deleting #{vnf_service.name} networks HOT orchestration template")
$evm.vmdb('orchestration_template_hot').destroy(template.id)
url = "http://localhost:3000/api/orchestration_templates/#{template.id}"
options = {:method => :delete,
:url => url,
:verify_ssl => false,
:headers => {"X-Auth-Token" => MIQ_API_TOKEN,
:accept => :json}}
RestClient::Request.execute(options)
end
end
end
Expand All @@ -78,7 +97,18 @@ def retire_vnfs(network_service)
end
end

def delete_stack(stack)
begin
$evm.log(:info, "Deleting stack #{stack}")
stack.raw_delete_stack()
rescue NotImplementedError => e
$evm.log(:info, "Stack #{stack} does not have a raw_delete_stack action")
end
end

begin
require 'rest-client'

nsd = $evm.get_state_var(:nsd)
network_service = nil
$evm.log("info", "Listing nsd #{nsd}")
Expand Down
Loading

0 comments on commit 36bbb82

Please sign in to comment.