Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INFCT-76 - Updated the plugin to support vRA 8 #63

Merged
merged 9 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 53 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
This is a Knife plugin that will allow you to interact with
VMware vRealize products, such as vRA and vRO, from Chef's Knife command.

Note: This version only support vRA 8.x. If you need to use this gem for vRA 7.x, try the 6.x version of this gem.

## Installation

Add this line to your application's Gemfile:
Expand Down Expand Up @@ -50,39 +52,40 @@ knife vra command --vra-username myuser --vra-tenant mytenant ...

#### knife vra catalog list

Lists catalog items that can be used to submit machine requests. By default, it will list all catalog items that your user has permission to see. To limit it to only items to which you are entitled, supply the `--entitled` flag.
Lists catalog items that can be used to submit machine requests.
By default, it will list all catalog items that your user has permission to see. To limit it to only items entitled to a particular project, supply the `--project-id PROJECT_ID` and `--entitled` flags.

```
$ knife vra catalog list
Catalog ID Name Description Status Subtenant
a9cd6148-6e0b-4a80-ac47-f5255c52b43d CentOS 6.6 Blueprint for deploying a CentOS Linux development server published
5dcd1900-3b89-433d-8563-9606ae1249b8 CentOS 6.6 - business group Blueprint for deploying a CentOS Linux development server published Rainpole Developers
d29efd6b-3cd6-4f8d-b1d8-da4ddd4e52b1 WindowsServer2012 Windows Server 2012 with the latest updates and patches. published
Catalog ID Name Description Source
a9cd6148-6e0b-4a80-ac47-f5255c52b43d CentOS 6.6 Blueprint for deploying a CentOS Linux development server Project-1
5dcd1900-3b89-433d-8563-9606ae1249b8 CentOS 6.6 - business group Blueprint for deploying a CentOS Linux development server Project-2
d29efd6b-3cd6-4f8d-b1d8-da4ddd4e52b1 WindowsServer2012 Windows Server 2012 with the latest updates and patches. Project-1
```

#### knife vra server list

Lists all machine resources that your user has permission to see. The "resource ID" is needed for other commands, such as `knife vra server show` and `knife vra server destroy`
Lists all machine resources that your user has permission to see. The "Deployment ID" is needed for other commands, such as `knife vra server show` and `knife vra server destroy`

```
$ knife vra server list
Resource ID Name Status Catalog Name
2e1f6632-1613-41d1-a07c-6137c9639609 hol-dev-43 active CentOS 6.6
43898686-7395-468a-99b3-b0b18a8abc1b hol-dev-44 active CentOS 6.6
0977f98b-d927-4e71-8b5b-b27c7deda097 hol-dev-45 active CentOS 6.6
Deployment ID Name Status Owner Description
9bfe77c9-0915-47b6-8479-8627b1b24ac2 Centos 8 create_successful admin Centos 8 created for testing
7f586519-3644-4c4a-a9de-3eb66a987993 Windows server 2012 delete_failed user1 Windows Server
00582a35-0365-40f1-8a47-e19579c6e5d5 Ubuntu 22.04 create_successful admin Test terraform
```

#### knife vra server show RESOURCE_ID
#### knife vra server show DEPLOYMENT_ID

Displays additional information about an individual server, such as its IP addresses.

```
$ knife vra server show 2e1f6632-1613-41d1-a07c-6137c9639609
Server ID: 2e1f6632-1613-41d1-a07c-6137c9639609
Server Name: hol-dev-43
IP Addresses: 192.168.110.203
Status: ACTIVE
Catalog Name: CentOS 6.6
$ knife vra server show 72fd5478-15f1-4aca-aa9b-012e2fa2ef01
Deployment ID: 72fd5478-15f1-4aca-aa9b-012e2fa2ef01
Deployment Name: Test terraform errors
IP Address: 10.30.237.66
Status: SUCCESS
Owner Names: admin
```

#### knife vra server create CATALOG_ID (options)
Expand All @@ -93,18 +96,18 @@ Each blueprint may require different parameters to successfully complete provisi

Common parameters to specify are:

* `--cpus`: number of CPUs
* `--memory`: amount of RAM in MB
* `--requested-for`: vRA login that should be listed as the owner
* `--lease-days`: number of days for the resource lease
* `--notes`: any optional notes you'd like to be logged with your request
* `--subtenant-id`: all resources must be tied back to a Business Group, or "subtenant." If your catalog item is tied to a specific Business Group, you do not need to specify this. However, if your catalog item is a global catalog item, then the subtenant ID is not available to us; you will need to provide it. It usually looks like a UUID. See your vRA administrator for assistance in determining your subtenant ID.
* `--image-mapping`: The image mapping that needed for this deployment which specifies the OS image for the vm
* `--flavor-mapping`: specifies the CPU count and RAM of a VM
* `--project-id`: Project ID also needs to be passed.
* `--name`: Can be used to specify the name of newly created deployment. This should be unique.
* `--version`: Specify which version of the catalog should be used for this deployment. If left blank, the latest version will be used.
* `--ssh-password`: if a linux host, the password to use during bootstrap
* `--winrm-password`: if a windows host, the password to use during bootstrap
* `--image-os-type`: windows/linux
* `--bootstrap-protocol`: winrm/ssh
* `--server-create-timeout`: increase this if your vRa environments takes more than 10 minutes to give you a server.
* `--bootstrap-version`: use to tie to a specific chef version if your group is not current
* `--extra_params`: a hash of other data to set on a catalog request, most notably custom properties. It should follow `key=type:value` format. eg: `--extra-param hardware-config=string:Micro`
* `-N`: node-name of the chef node to create. The gem will automatically create a node name with prefix `vra-` if not specified

Most of these can be set in your `knife.rb` to simplify the command:
Expand All @@ -127,31 +130,42 @@ knife[:winrm_password] = 'machine-account-pass'


```
$ knife vra server create 5dcd1900-3b89-433d-8563-9606ae1249b8 --cpus 1 --memory 512 --requested-for devmgr@corp.local --ssh-password 'mypassword' --lease-days 5
Catalog request d282fde8-6fd2-406c-998e-328d1b659078 submitted.
$ knife vra server create 24026193-5863-3f72-baac-7f4cd3e1d535 --name testing-centos --project-id pro-123 \
--image-mapping VRA-nc-lnx-ce8.0 --flavor-mapping Micro --image-os-type linux --connection-protocol ssh \
-P password --extra-param hardware-config=string:Micro
Catalog request b1f13afe-d7c1-4647-8866-30681fc7f63d submitted.
Waiting for request to complete.
Current request status: PENDING_PRE_APPROVAL.
Current request status: IN_PROGRESS..
Current request status: CREATE_INPROGRESS.....................................
Catalog request complete.

Request Status: CREATE_SUCCESSFUL

Deployment ID: b1f13afe-d7c1-4647-8866-30681fc7f63d
Deployment Name: test_dep-2
IP Address: 10.30.236.21
Owner Names: admin
Bootstrapping the server by using connection_protocol: ssh and image_os_type: linux

Waiting for sshd to host (10.30.236.21)............
...
```

#### knife vra server delete RESOURCE_ID
#### knife vra server delete DEPLOYMENT_ID

Deletes a server from vRA. If you supply `--purge`, the server will also be removed from the Chef Server.
Deletes a deployment and associated resources from vRA. If you supply `--purge`, the server will also be removed from the Chef Server.

```
$ knife vra server delete 2e1f6632-1613-41d1-a07c-6137c9639609 --purge
Server ID: 2e1f6632-1613-41d1-a07c-6137c9639609
Server Name: hol-dev-43
IP Addresses: 192.168.110.203
Status: ACTIVE
Catalog Name: CentOS 6.6

Do you really want to delete this server? (Y/N) Y
Destroy request f2aa716b-ab24-4232-ac4a-07635a03b4d4 submitted.
Deployment ID: 2e1f6632-1613-41d1-a07c-6137c9639609
Deployment Name: test_dep-2
IP Address: 10.30.236.21
Status: SUCCESS
Owner Names: ${userName}

Do you really want to delete this server? (Y/N) y
Destroy request 5e390a9d-1340-489d-94be-b4eb1df98c53 submitted.
Waiting for request to complete.
Current request status: PENDING_PRE_APPROVAL.
Current request status: IN_PROGRESS...
Current request status: CHECKING_APPROVAL...
...
```

Expand Down
2 changes: 1 addition & 1 deletion knife-vrealize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "knife"
spec.add_dependency "knife-cloud", ">= 1.2.0", "< 5.0"
spec.add_dependency "vmware-vra", "~> 2", "< 3" # 3 and above is not supported for this version of vRA
spec.add_dependency "vmware-vra", "~> 3"
spec.add_dependency "vcoworkflows", "~> 0.2"
spec.add_dependency "rb-readline", "~> 0.5"
end
51 changes: 27 additions & 24 deletions lib/chef/knife/cloud/vra_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def connection
def create_server(options = {})
submitted_request = catalog_request(options).submit
ui.msg("Catalog request #{submitted_request.id} submitted.")
wait_for_request(submitted_request, options[:wait_time].to_i, options[:refresh_rate])
wait_for_request(submitted_request, (options[:wait_time] || 600).to_i, options[:refresh_rate] || 2)
ui.msg("Catalog request complete.\n")
request_summary(submitted_request)

raise CloudExceptions::ServerCreateError, submitted_request.completion_details if submitted_request.failed?
raise CloudExceptions::ServerCreateError if submitted_request.failed?

servers = submitted_request.resources.select(&:vm?)
raise CloudExceptions::ServerCreateError, "The vRA request created more than one server, but we were only expecting 1" if servers.length > 1
Expand All @@ -67,8 +67,9 @@ def create_server(options = {})
servers.first
end

def delete_server(instance_id)
server = get_server(instance_id)
def delete_server(deployment_id)
deployment = get_deployment(deployment_id)
server = deployment.resources.select(&:vm?).first
sanjain-progress marked this conversation as resolved.
Show resolved Hide resolved
server_summary(server)
ui.msg("")

Expand All @@ -79,56 +80,58 @@ def delete_server(instance_id)

ui.confirm("Do you really want to delete this server")

destroy_request = server.destroy
destroy_request = deployment.destroy
ui.msg("Destroy request #{destroy_request.id} submitted.")
wait_for_request(destroy_request)
ui.msg("Destroy request complete.")
request_summary(destroy_request)
end

def list_servers
connection.resources.all_resources.select(&:vm?)
connection.deployments.all
end

def list_catalog_items(entitled)
def list_catalog_items(project_id, entitled)
if entitled
connection.catalog.entitled_items
connection.catalog.entitled_items(project_id)
else
connection.catalog.all_items
end
end

def get_server(instance_id)
connection.resources.by_id(instance_id)
def get_deployment(deployment_id)
connection.deployments.by_id(deployment_id)
end

def get_server(deployment_id)
deployment = connection.deployments.by_id(deployment_id)
deployment.resources.select(&:vm?).first
end

def server_summary(server, _columns_with_info = nil)
msg_pair("Server ID", server.id)
msg_pair("Server Name", server.name)
msg_pair("IP Addresses", server.ip_addresses.nil? ? "none" : server.ip_addresses.join(", "))
deployment = connection.deployments.by_id(server.deployment_id)
msg_pair("Deployment ID", deployment.id)
msg_pair("Deployment Name", deployment.name)
msg_pair("IP Address", server.ip_address.nil? ? "none" : server.ip_address)
msg_pair("Status", server.status)
msg_pair("Catalog Name", server.catalog_name)
msg_pair("Owner IDs", server.owner_ids.empty? ? "none" : server.owner_ids.join(", "))
msg_pair("Owner Names", server.owner_names.empty? ? "none" : server.owner_names.join(", "))
msg_pair("Owner Names", server.owner_names.empty? ? "none" : server.owner_names)
end

def request_summary(request)
ui.msg("")
msg_pair("Request Status", request.status)
msg_pair("Completion State", request.completion_state)
msg_pair("Completion Details", request.completion_details)
ui.msg("")
end

def catalog_request(options)
catalog_request = connection.catalog.request(options[:catalog_id])

catalog_request.cpus = options[:cpus]
catalog_request.memory = options[:memory]
catalog_request.requested_for = options[:requested_for]
catalog_request.lease_days = options[:lease_days] unless options[:lease_days].nil?
catalog_request.notes = options[:notes] unless options[:notes].nil?
catalog_request.subtenant_id = options[:subtenant_id] unless options[:subtenant_id].nil?
catalog_request.image_mapping = options[:image_mapping]
catalog_request.flavor_mapping = options[:flavor_mapping]
catalog_request.name = options[:name]
catalog_request.project_id = options[:project_id]
catalog_request.version = options[:version] unless options[:version].nil?

options[:extra_params]&.each do |param|
catalog_request.set_parameter(param[:key], param[:type], param[:value])
end
Expand Down
9 changes: 6 additions & 3 deletions lib/chef/knife/vra_catalog_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class VraCatalogList < ResourceListCommand
include VraServiceHelpers
end

option :project_id,
long: "--project-id",
description: "Catalogs are retrieved using the Project ID"

option :entitled,
long: "--entitled-only",
description: "only list entitled vRA catalog entries",
Expand All @@ -47,15 +51,14 @@ def before_exec_command
{ label: "Catalog ID", key: "id" },
{ label: "Name", key: "name" },
{ label: "Description", key: "description" },
{ label: "Status", key: "status", value_callback: method(:format_status_value) },
{ label: "Subtenant", key: "subtenant_name" },
{ label: "Source", key: "source_name" },
]

@sort_by_field = "name"
end

def query_resource
@service.list_catalog_items(config[:entitled])
@service.list_catalog_items(config[:project_id], config[:entitled])
end

def format_status_value(status)
Expand Down
55 changes: 22 additions & 33 deletions lib/chef/knife/vra_server_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,30 @@ class VraServerCreate < ServerCreateCommand
require_relative "cloud/vra_service"
end

option :cpus,
long: "--cpus NUM_CPUS",
description: "Number of CPUs the server should have"
option :project_id,
long: "--project-id PROJECT_ID",
description: "ID of the project"

option :node_ssl_verify_mode,
long: "--node-ssl-verify-mode [peer|none]",
description: "Whether or not to verify the SSL cert for all HTTPS requests when bootstrapping"
option :memory,
long: "--memory RAM_IN_MB",
description: "Amount of RAM, in MB, the server should have"

option :requested_for,
long: "--requested-for LOGIN",
description: "The login to list as the owner of this resource. Will default to the vra_username parameter"
option :image_mapping,
long: "--image-mapping IMAGE_MAPPING",
description: "Specifies the OS image for the new VM"

option :server_create_timeout,
long: "--server-create-timeout SECONDS",
description: "number of seconds to wait for the server to complete",
default: 600

option :subtenant_id,
long: "--subtenant-id ID",
description: 'The subtenant ID (a.k.a "business group") to list as the owner of this resource. ' \
"Will default to the blueprint subtenant if it exists."
option :flavor_mapping,
long: "--flavor-mapping FLAVOR_MAPPING",
description: "Specifies the CPU count and RAM for the new VM"

option :lease_days,
long: "--lease-days NUM_DAYS",
description: "Number of days requested for the server lease, provided the blueprint allows this to be specified"
option :version,
long: "--version VERSION",
description: "Specifies the version of the catalog to be used. By default the latest version will be used."

option :notes,
long: "--notes NOTES",
description: "String of text to be included in the request notes."
option :name,
long: "--name NAME",
description: "Name for the newly created deployment"

option :extra_params,
long: "--extra-param KEY=TYPE:VALUE",
Expand All @@ -90,7 +82,7 @@ def validate_params!
exit 1
end

check_for_missing_config_values!(:cpus, :memory, :requested_for)
check_for_missing_config_values!(:name, :flavor_mapping, :image_mapping, :project_id)

validate_extra_params!
end
Expand All @@ -100,15 +92,12 @@ def before_exec_command

@create_options = {
catalog_id: @name_args.first,
cpus: config[:cpus],
memory: config[:memory],
requested_for: config[:requested_for],
subtenant_id: config[:subtenant_id],
lease_days: config[:lease_days],
notes: config[:notes],
project_id: config[:project_id],
image_mapping: config[:image_mapping],
flavor_mapping: config[:flavor_mapping],
version: config[:version],
name: config[:name],
extra_params: extra_params,
wait_time: config[:server_create_timeout],
refresh_rate: config[:request_refresh_rate],
}
end

Expand Down Expand Up @@ -139,7 +128,7 @@ def validate_extra_params!
end

def hostname_for_server
ip_address = server.ip_addresses.first
ip_address = server.ip_address

ip_address.nil? ? server.name : ip_address
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/vra_server_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VraServerDelete < ServerDeleteCommand
include ServerDeleteOptions
include VraServiceOptions

banner "knife vra server delete RESOURCE_ID [RESOURCE_ID] (options)"
banner "knife vra server delete DEPLOYMENT_ID [DEPLOYMENT_ID] (options)"

deps do
require_relative "cloud/vra_service"
Expand Down
Loading