Skip to content

Commit

Permalink
Merge pull request #1411 from JunqiZhang0/add-api-azure
Browse files Browse the repository at this point in the history
Merged by Jenkins
  • Loading branch information
paas-bot authored Dec 8, 2019
2 parents fe96fbc + f32df0b commit 3eb44c3
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 12 deletions.
5 changes: 3 additions & 2 deletions config/Dockerfiles/linchpin-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TESTS_DIR="./config/Dockerfiles/tests.d"


if [ -z "${PROVIDERS}" ]; then
PROVIDERS="dummy libvirt beaker openstack"
PROVIDERS="dummy libvirt beaker openstack azure aws"
fi

# this function checks the distros.exclude value within the test.
Expand Down Expand Up @@ -83,6 +83,7 @@ result=0

export CREDS_PATH="$base_dir/keys"
pushd "${CREDS_PATH}"
subscriptionid="$(sed 's/subscription_id: //' azure.key |tail -n 1)"
for provider in ${PROVIDERS}; do
# If CREDS_PATH provides a tarball extract it and
# run it's install script
Expand Down Expand Up @@ -112,7 +113,7 @@ for testdir in *; do
echo >> ${base_dir}/${distro}_logs/${provider}.log
echo "==== TEST: ${testname} ====" | tee -a ${base_dir}/${distro}_logs/${provider}.log
pushd "${base_dir}" &> /dev/null
${TESTS_DIR}/${testdir}/${test} ${distro} ${provider} 2>&1 | tee -a ${base_dir}/${distro}_logs/${provider}.log
${TESTS_DIR}/${testdir}/${test} ${distro} ${provider} ${subscriptionid} 2>&1 | tee -a ${base_dir}/${distro}_logs/${provider}.log
RC=${?}
if [ ${RC} -eq 0 ]; then
test_summary="$(tput setaf 2)SUCCESS$(tput sgr0)\t${testname}"
Expand Down
2 changes: 1 addition & 1 deletion config/Dockerfiles/tests.d/azure/03_azure_virtual_subnet
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash -xe

# Verify the azure vm provisioning
# distros.exclude: fedora29 fedora30 centos7
# distros.exclude: none
# providers.include: azure
# providers.exclude: none

Expand Down
2 changes: 1 addition & 1 deletion config/Dockerfiles/tests.d/azure/04_azure_vm_public_image
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash -xe

# Verify the azure vm provisioning
# distros.exclude: fedora29 fedora30 centos7
# distros.exclude: none
# providers.include: azure
# providers.exclude: none

Expand Down
37 changes: 37 additions & 0 deletions config/Dockerfiles/tests.d/azure/05_azure_api
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash -xe

# Verify the azure vm provisioning
# distros.exclude: none
# providers.include: azure
# providers.exclude: none

DISTRO=${1}
PROVIDER=${2}
subscriptionid=${3}
resourcegroupname="${DISTRO}-LinchpinCreatedGroup"
providers='compute'
TARGET="azure_api"
vmname="${1}test"
body_path="/workDir/workspace/ci-linchpin/linchpin/docs/source/examples/workspaces/azure/body.json"
pushd docs/source/examples/workspaces/${PROVIDER}
function clean_up {
set +e
linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}', "subscriptionid": '${subscriptionid}',"resourcegroupname": '${resourcegroupname}',"providers": '${providers}',"vmname": '${vmname}',"body_path": '${body_path}'}' destroy "${TARGET}"
linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}'}' destroy "azure_nic"
linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}""'}' destroy "azure_vn"
sed "s/\(\"id\".*\/\).*/\1TestingNIC\",/" ${body_path} -i
sed "s/${resourcegroupname}/REDSOURCEGROUPPLACEHOLDER/" ${body_path} -i
sed "s/${subscriptionid}/SUBSCRIPTIONPLACEHOLDER/" ${body_path} -i
rm -rf resources/ inventories/
}

trap clean_up EXIT

linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}""'}' up "azure_vn"
linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}""'}' up "azure_vn_subnet"
linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}'}' up "azure_nic"
sed "s/\(\"id\".*\/\).*/\1${DISTRO}-forLinchpinautomation\",/" ${body_path} -i
sed "s/REDSOURCEGROUPPLACEHOLDER/${resourcegroupname}/" ${body_path} -i
sed "s/SUBSCRIPTIONPLACEHOLDER/${subscriptionid}/" ${body_path} -i
cat ${body_path}
linchpin -w . -vvv --template-data '{ "distro": '${DISTRO}', "subscriptionid": '${subscriptionid}',"resourcegroupname": '${resourcegroupname}',"providers": '${providers}',"vmname": '${vmname}',"body_path": '${body_path}'}' up "${TARGET}"
2 changes: 1 addition & 1 deletion config/Dockerfiles/tests.d/azure/08_azure_multi_vm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash -xe

# Verify the azure vm provisioning
# distros.exclude: fedora30 centos7 fedora31
# distros.exclude: none
# providers.include: azure
# providers.exclude: none

Expand Down
36 changes: 36 additions & 0 deletions docs/source/azure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ definition, the following options are available.

⚫ If you declare both public and private image, only the private will be taken

azure_api
-------

Any Azure resources can be provisioned using this role, it supported by the Azure Api

* Example <workspaces/azure/Pinfile>`
* azure_api module <https://docs.ansible.com/ansible/latest/modules/azure_rm_resource_module.html#azure-rm-resource-module>`_
* Azure API <https://docs.microsoft.com/en-us/rest/api/?view=Azure>`_

Topology Schema
~~~~~~~~~~~~~~~

Within Linchpin, the :term:`azure_api` :term:`resource_definition` has more
options than what is shown in the examples above. For each :term:`azure_api`
definition, the following options are available.

+----------------------+------------+---------------+-----------------------+--------------------+
| Parameter | required | type | ansible value | comments |
+======================+============+===============+=======================+====================+
| role | true | string | N/A | |
+----------------------+------------+---------------+-----------------------+--------------------+
| resource_group | true |String | resource_group | |
+----------------------+------------+---------------+-----------------------+--------------------+
| resource_type | true |string | resource_type | |
+----------------------+------------+---------------+-----------------------+--------------------+
| resource_name | true |string | resource_name | |
+----------------------+------------+---------------+-----------------------+--------------------+
| api_version |true | string | api_version | |
+----------------------+------------+---------------+-----------------------+--------------------+
| body_path |true | string | |Path to request body|
+----------------------+------------+---------------+-----------------------+--------------------+
| url |true | string |url | |
+----------------------+------------+---------------+-----------------------+--------------------+



Credentials Management
----------------------
Support IAM user (instruction below)
Expand Down
18 changes: 17 additions & 1 deletion docs/source/examples/workspaces/azure/PinFile
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
---
---
azure_api:
topology:
topology_name: azure_api
resource_groups:
- resource_group_name: "azure_api"
resource_group_type: "azure"
resource_definitions:
- role: "azure_api"
resource_group: "{{ distro }}-LinchpinCreatedGroup"
body_path: {{body_path}}
#You can specify the name and path of body.json
url: "https://management.azure.com/subscriptions/{{subscriptionid}}/resourceGroups/{{resourcegroupname}}/providers/Microsoft.{{providers}}/virtualMachines/{{vmname}}?api-version=2019-03-01"
credentials:
filename: azure.key
profile: key

azure_vm_public_image:
topology:
topology_name: azure_vm_public_image
Expand Down
39 changes: 39 additions & 0 deletions docs/source/examples/workspaces/azure/body.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"location": "centralus",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_D1_v2"
},
"storageProfile": {
"imageReference": {
"sku": "7.1",
"publisher": "OpenLogic",
"version": "latest",
"offer": "CentOS"
},
"osDisk": {
"caching": "ReadWrite",
"managedDisk": {
"storageAccountType": "Standard_LRS"
},
"name": "myVMosdisk",
"createOption": "FromImage"
}
},
"osProfile": {
"adminUsername": "Linchpin",
"computerName": "myVM",
"adminPassword": "IloveLinchpin!"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/SUBSCRIPTIONPLACEHOLDER/resourceGroups/REDSOURCEGROUPPLACEHOLDER/providers/Microsoft.Network/networkInterfaces/TestingNIC",
"properties": {
"primary": true
}
}
]
}
}
}
44 changes: 40 additions & 4 deletions linchpin/provision/roles/azure/files/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
"required": false
},
"deepclean": {
"type":"boolean",
"required":false
"type": "boolean",
"required": false
},
"delete_all_attached":{
"delete_all_attached": {
"type": "string",
"required": false
},
Expand Down Expand Up @@ -208,8 +208,44 @@
"required": false
}
}
},
{
"type": "dict",
"schema": {
"role": {
"type": "string",
"required": true,
"allowed": [
"azure_api"
]
},
"resource_type": {
"type": "string",
"required": false
},
"url": {
"type": "string",
"required": false
},
"resource_group": {
"type": "string",
"required": false
},
"resource_name": {
"type": "string",
"required": false
},
"api_version": {
"type": "string",
"required": false
},
"body_path": {
"type": "string",
"required": false
}
}
}
]
}
}
}
}
1 change: 1 addition & 0 deletions linchpin/provision/roles/azure/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
topology_outputs_azure_vn_subnet: []
topology_outputs_azure_res_grp: []
topology_outputs_azure_net_interface: []
topology_outputs_azure_api: []

- name: "Initiating Provisioning of Azure resource group"
include: provision_resource_group.yml res_grp={{ item }}
Expand Down
30 changes: 30 additions & 0 deletions linchpin/provision/roles/azure/tasks/provision_azure_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
- name: set a body path
set_fact:
body_path: "{{res_def['body_path']|default('{{workspace}}/body.json')}}"

- debug:
msg: "{{ res_def['url']}}"

- name: Use Azure Api to manage resources
azure_rm_resource:
client_id: "{{ auth_var['client_id'] | default(omit) }}"
tenant: "{{ auth_var['tenant'] | default(omit) }}"
secret: "{{ auth_var['secret'] | default(omit) }}"
subscription_id: "{{ auth_var['subscription_id'] | default(omit) }}"
api_version: "{{res_def['api_version']|default('2019-03-01')}}"
url: "{{ res_def['url']|default(omit)}}"
body: "{{ lookup('file', body_path) }}"
register: res_def_output
until: res_def_output.response.properties.provisioningState == "Updating"
retries: 30
delay: 30

- name: "Append outputitem to topology_outputs"
set_fact:
topology_outputs_azure_api: "{{ topology_outputs_azure_api + [res_def_output] }}"
when: res_def_output['changed'] == true

- name: "Add type to resource"
set_fact:
topology_outputs_azure_api: "{{ topology_outputs_azure_api | add_res_data(lookup('vars', 'role_name'),'azure_api') }}"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- name: "Provisioning Azure Virtual Network when not async"
- name: "Provisioning Azure Virtual Network Interface when not async"
azure_rm_networkinterface:
client_id: "{{ auth_var['client_id'] | default(omit) }}"
tenant: "{{ auth_var['tenant'] | default(omit) }}"
Expand Down
17 changes: 17 additions & 0 deletions linchpin/provision/roles/azure/tasks/teardown_azure_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: set a body path
set_fact:
body_path: "{{res_def['body_path']|default('{{workspace}}/body.json')}}"

- name: Use Azure Api to manage resources
azure_rm_resource:
client_id: "{{ auth_var['client_id'] | default(omit) }}"
tenant: "{{ auth_var['tenant'] | default(omit) }}"
secret: "{{ auth_var['secret'] | default(omit) }}"
subscription_id: "{{ auth_var['subscription_id'] | default(omit) }}"
api_version: "{{res_def['api_version']|default('2019-03-01')}}"
url: "{{ res_def['url']|default(omit)}}"
method: 'DELETE'
polling_timeout: 60
polling_interval: 10
body: "{{ lookup('file', body_path) }}"
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
subnet_name: "{{ res_def['subnet_name']|default(omit) }}"
name: "{{ res_def['network_interface_name']|default('network_interface_name') }}"
state: absent
register: res_def_output
register: res_def_output
5 changes: 5 additions & 0 deletions linchpin/provision/roles/gather_resources/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@
topology_outputs: "{{ topology_outputs + topology_outputs_azure_net_interface }}"
when: topology_outputs_azure_net_interface is defined

- name: "Add azure_api_res"
set_fact:
topology_outputs: "{{ topology_outputs + topology_outputs_azure_api }}"
when: topology_outputs_azure_api is defined

- name: "Add azure_res_grp_res"
set_fact:
topology_outputs: "{{ topology_outputs + topology_outputs_azure_res_grp }}"
Expand Down

0 comments on commit 3eb44c3

Please sign in to comment.