Skip to content

Commit

Permalink
Add support for custom UA prefixes (linode#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
LBGarber authored Oct 13, 2022
1 parent c9986c6 commit 3a83ae2
Show file tree
Hide file tree
Showing 39 changed files with 196 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ $(INTEGRATION_CONFIG):
echo "LINODE_API_TOKEN must be set"; \
exit 1; \
fi
echo "api_token: $(LINODE_API_TOKEN)" >> $(INTEGRATION_CONFIG)
echo "api_token: $(LINODE_API_TOKEN)" > $(INTEGRATION_CONFIG)
echo "ua_prefix: E2E" >> $(INTEGRATION_CONFIG)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Once the Linode Ansible collection is installed, it can be referenced by its [Fu
In order to use this collection, the `LINODE_API_TOKEN` environment variable must be set to a valid Linode API v4 token.
Alternatively, you can pass your Linode API v4 token into the `api_token` option for each Linode module you reference.

The `LINODE_UA_PREFIX` or the `ua_prefix` module option can be used to specify a custom User-Agent prefix.

#### Example Playbook
```yaml
---
Expand Down
15 changes: 14 additions & 1 deletion plugins/module_utils/linode_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
required=True,
choices=['present', 'absent'],
),
ua_prefix=dict(
type='str',
description='An HTTP User-Agent Prefix to prepend in API requests.',
doc_hide=True,
fallback=(env_fallback, ['LINODE_UA_PREFIX'])
)
)

LINODE_TAG_ARGS = dict(
Expand Down Expand Up @@ -132,10 +138,17 @@ def client(self) -> LinodeClient:
api_token = self.module.params['api_token']
api_version = self.module.params['api_version']

user_agent = COLLECTION_USER_AGENT

# Allow for custom user-agent prefixes
ua_prefix = self.module.params['ua_prefix']
if ua_prefix is not None:
user_agent = f"{ua_prefix} {user_agent}"

self._client = LinodeClient(
api_token,
base_url='https://api.linode.com/{0}'.format(api_version),
user_agent=COLLECTION_USER_AGENT,
user_agent=user_agent,
retry_rate_limit_interval=10,
)

Expand Down
2 changes: 2 additions & 0 deletions template/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Once the Linode Ansible collection is installed, it can be referenced by its [Fu
In order to use this collection, the `LINODE_API_TOKEN` environment variable must be set to a valid Linode API v4 token.
Alternatively, you can pass your Linode API v4 token into the `api_token` option for each Linode module you reference.

The `LINODE_UA_PREFIX` or the `ua_prefix` module option can be used to specify a custom User-Agent prefix.

#### Example Playbook
```yaml
---
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/account_info/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# - name: Get info about the current account
# linode.cloud.account_info:
# api_token: '{{ api_token }}'
# ua_prefix: '{{ ua_prefix }}'
# register: account
#
# - assert:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/targets/domain_basic/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- name: Create a domain
linode.cloud.domain:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
description: 'really cool domain'
domain: 'ansible-test-domain-{{ r }}.com'
expire_sec: 300
Expand Down Expand Up @@ -35,6 +36,7 @@
- name: Update a domain
linode.cloud.domain:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
description: 'really cool'
domain: '{{ create.domain.domain }}'
expire_sec: 14400
Expand Down Expand Up @@ -64,6 +66,7 @@
- name: Get domain info
linode.cloud.domain_info:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ create.domain.domain }}'
register: info

Expand All @@ -85,6 +88,7 @@
- name: Delete the domain
linode.cloud.domain:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ create.domain.domain }}'
state: absent
register: delete
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/targets/domain_record/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- name: Create a domain
linode.cloud.domain:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: 'ansible-test-domain-{{ r }}.com'
soa_email: '[email protected]'
type: 'master'
Expand All @@ -21,6 +22,7 @@
- name: Create a domain record
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
name: 'sub'
type: 'A'
Expand All @@ -41,6 +43,7 @@
- name: Update the domain record
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
record_id: '{{ record_create.record.id }}'
ttl_sec: 14400
Expand All @@ -60,6 +63,7 @@
- name: Get info about the record
linode.cloud.domain_record_info:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
name: '{{ record_create.record.name }}'
register: record_info
Expand All @@ -75,6 +79,7 @@
- name: Get info about the record by id
linode.cloud.domain_record_info:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
id: '{{ record_create.record.id }}'
register: record_info_id
Expand All @@ -90,6 +95,7 @@
- name: Create a domain record by domain id
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain_id: '{{ domain_create.domain.id }}'
name: 'cool'
type: 'TXT'
Expand All @@ -110,6 +116,7 @@
- name: Create a duplicate record with a different target
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
name: 'sub'
type: 'A'
Expand All @@ -130,6 +137,7 @@
- name: Update the record by id
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
record_id: '{{ record_dupe_create.record.id }}'
ttl_sec: 300
Expand All @@ -149,6 +157,7 @@
- name: Get all domain records
linode.cloud.domain_info:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
register: domain_info

Expand All @@ -162,6 +171,7 @@
- name: Delete the record
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
record_id: '{{ record_create.record.id }}'
state: absent
Expand All @@ -175,6 +185,7 @@
- name: Delete the second record
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
name: '{{ record2_create.record.name }}'
type: '{{ record2_create.record.type }}'
Expand All @@ -190,6 +201,7 @@
- name: Delete the duplicated record
linode.cloud.domain_record:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
record_id: '{{ record_dupe_create.record.id }}'
state: absent
Expand All @@ -203,6 +215,7 @@
- name: Delete the domain
linode.cloud.domain:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
domain: '{{ domain_create.domain.domain }}'
state: absent
register: domain_delete
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/event_list/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- name: Create an arbitrary event
linode.cloud.stackscript:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}'
images: ['any/all']
script: |
Expand All @@ -17,6 +18,7 @@
- name: List the events for the current Linode Account
linode.cloud.event_list:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
count: 5
register: no_filter

Expand All @@ -28,6 +30,7 @@
- name: Resolve the StackScript event
linode.cloud.event_list:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
count: 1
order_by: created
order: desc
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/targets/firewall_basic/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- name: Create a Linode Instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}'
region: us-southeast
type: g6-standard-1
Expand All @@ -15,6 +16,7 @@
- name: Create another Linode Instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}-2'
region: us-southeast
type: g6-standard-1
Expand All @@ -25,6 +27,7 @@
- name: Create a Linode Firewall
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: 'ansible-test-{{ r }}'
devices: []
Expand All @@ -44,6 +47,7 @@
- name: Update a Linode Firewall
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
devices: []
Expand All @@ -65,6 +69,7 @@
- name: Update Linode Firewall devices
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
devices:
Expand All @@ -90,6 +95,7 @@
- name: Update Linode Firewall devices again
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
devices:
Expand All @@ -115,6 +121,7 @@
- name: Update Linode Firewall devices unchanged
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
devices:
Expand All @@ -137,6 +144,7 @@
- name: Update Linode Firewall rules
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
devices: []
Expand Down Expand Up @@ -188,6 +196,7 @@
- name: Update a Linode Firewall rules unchanged
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
devices: []
Expand Down Expand Up @@ -223,6 +232,7 @@
- name: Get info about the firewall
linode.cloud.firewall_info:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
register: firewall_info
Expand Down Expand Up @@ -254,6 +264,7 @@
- name: Delete a Linode Firewall
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: '{{ create.firewall.label }}'
state: absent
Expand All @@ -268,6 +279,7 @@
- name: Delete a Linode instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: '{{ create_instance.instance.label }}'
state: absent
register: delete_instance
Expand All @@ -281,6 +293,7 @@
- name: Delete a Linode instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: '{{ create_instance_2.instance.label }}'
state: absent
register: delete_instance_2
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/targets/firewall_device/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- name: Create a Linode Instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}'
region: us-southeast
type: g6-standard-1
Expand All @@ -16,6 +17,7 @@
- name: Create a Linode Firewall
linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
label: 'ansible-test-{{ r }}'
devices: []
Expand All @@ -30,6 +32,7 @@
- name: Add Device to Linode Firewall
linode.cloud.firewall_device:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
firewall_id: '{{ fw.firewall.id }}'
entity_id: '{{ inst.instance.id }}'
Expand All @@ -45,6 +48,7 @@
- name: Add Existing Device to Linode Firewall
linode.cloud.firewall_device:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
api_version: v4beta
firewall_id: '{{ fw.firewall.id }}'
entity_id: '{{ inst.instance.id }}'
Expand All @@ -62,17 +66,20 @@
block:
- linode.cloud.firewall_device:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
firewall_id: '{{ fw.firewall.id }}'
entity_id: '{{ inst.instance.id }}'
entity_type: 'linode'
state: absent

- linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: '{{ inst.instance.label }}'
state: absent

- linode.cloud.firewall:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: '{{ fw.firewall.label }}'
state: absent
Loading

0 comments on commit 3a83ae2

Please sign in to comment.