Skip to content

Commit

Permalink
Merge pull request #637 from linode/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yec-akamai authored Jan 7, 2025
2 parents 95db231 + 4da84f0 commit e5e7e9f
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 38 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "CodeQL Advanced"

on:
push:
branches: [ "dev", "main", "proj/*" ]
pull_request:
branches: [ "dev", "main", "proj/*" ]
schedule:
- cron: '0 13 * * 5'

jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-latest
permissions:
security-events: write

strategy:
fail-fast: false
matrix:
include:
- language: python
build-mode: none

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
queries: security-and-quality

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
19 changes: 19 additions & 0 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'Dependency review'
on:
pull_request:
branches: [ "dev", "main", "proj/*" ]

permissions:
contents: read
pull-requests: write

jobs:
dependency-review:
runs-on: ubuntu-latest
steps:
- name: 'Checkout repository'
uses: actions/checkout@v4
- name: 'Dependency Review'
uses: actions/dependency-review-action@v4
with:
comment-summary-in-pr: on-failure
12 changes: 0 additions & 12 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ jobs:
with:
path: .ansible/collections/ansible_collections/linode/cloud

- name: update packages
run: sudo apt-get update -y

- name: install packages
run: sudo apt-get install -y make

- name: setup python 3
uses: actions/setup-python@v5
with:
Expand Down Expand Up @@ -60,12 +54,6 @@ jobs:
with:
path: .ansible/collections/ansible_collections/linode/cloud

- name: update packages
run: sudo apt-get update -y

- name: install packages
run: sudo apt-get install -y make

- name: setup python 3
uses: actions/setup-python@v5
with:
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/integration-tests-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ jobs:
fetch-depth: 0
submodules: 'recursive'

# Install deps
- name: update packages
run: sudo apt-get update -y

- name: install make
run: sudo apt-get install -y build-essential

- name: setup python 3
uses: actions/setup-python@v5
with:
Expand All @@ -55,7 +48,7 @@ jobs:
run: make deps

- name: install ansible dependencies
run: ansible-galaxy collection install amazon.aws:==6.0.1
run: ansible-galaxy collection install amazon.aws:==9.1.0

- name: install collection
run: make install
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ jobs:
fetch-depth: 0
submodules: 'recursive'

- name: Update packages
run: sudo apt-get update -y

- name: Setup Python 3
uses: actions/setup-python@v5
with:
Expand All @@ -52,7 +49,7 @@ jobs:
run: make deps

- name: Install ansible dependencies
run: ansible-galaxy collection install amazon.aws:==6.0.1
run: ansible-galaxy collection install amazon.aws:==9.1.0

- name: Install Collection
run: make install
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/nightly-smoke-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ jobs:
fetch-depth: 0
submodules: 'recursive'

- name: Update packages
run: sudo apt-get update -y

- name: Setup Python 3
uses: actions/setup-python@v5
with:
Expand All @@ -40,7 +37,7 @@ jobs:
run: make deps

- name: Install ansible dependencies
run: ansible-galaxy collection install amazon.aws:==6.0.1
run: ansible-galaxy collection install amazon.aws:==9.1.0

- name: Install Collection
run: make install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
run: make deps

- name: install ansible dependencies
run: ansible-galaxy collection install amazon.aws:==6.0.1
run: ansible-galaxy collection install amazon.aws:==9.1.0

- name: inject docs using specdoc
run: make inject
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Name | Description |
[linode.cloud.firewall_device](./docs/modules/firewall_device.md)|Manage Linode Firewall Devices.|
[linode.cloud.image](./docs/modules/image.md)|Manage a Linode Image.|
[linode.cloud.instance](./docs/modules/instance.md)|Manage Linode Instances, Configs, and Disks.|
[linode.cloud.ip](./docs/modules/ip.md)|Allocates a new IPv4 Address on your Account. The Linode must be configured to support additional addresses - please Open a support ticket requesting additional addresses before attempting allocation.|
[linode.cloud.ip_assign](./docs/modules/ip_assign.md)|Assign IPs to Linodes in a given Region.|
[linode.cloud.ip_rdns](./docs/modules/ip_rdns.md)|Manage a Linode IP address's rDNS.|
[linode.cloud.ip_share](./docs/modules/ip_share.md)|Manage the Linode shared IPs.|
Expand Down
38 changes: 38 additions & 0 deletions docs/modules/ip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ip

Allocates a new IPv4 Address on your Account. The Linode must be configured to support additional addresses - please Open a support ticket requesting additional addresses before attempting allocation.

- [Minimum Required Fields](#minimum-required-fields)
- [Examples](#examples)
- [Parameters](#parameters)
- [Return Values](#return-values)

## Minimum Required Fields
| Field | Type | Required | Description |
|-------------|-------|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `api_token` | `str` | **Required** | The Linode account personal access token. It is necessary to run the module. <br/>It can be exposed by the environment variable `LINODE_API_TOKEN` instead. <br/>See details in [Usage](https://github.com/linode/ansible_linode?tab=readme-ov-file#usage). |

## Examples

```yaml
- name: Allocate IP to Linode
linode.cloud.ip:
linode_id: 123
public: true
type: ipv4
state: present
```
## Parameters
| Field | Type | Required | Description |
|-----------|------|----------|------------------------------------------------------------------------------|
| `state` | <center>`str`</center> | <center>**Required**</center> | The state of this IP. **(Choices: `present`, `absent`)** |
| `linode_id` | <center>`int`</center> | <center>Optional</center> | The ID of a Linode you have access to that this address will be allocated to. |
| `public` | <center>`bool`</center> | <center>Optional</center> | Whether to create a public or private IPv4 address. |
| `type` | <center>`str`</center> | <center>Optional</center> | The type of address you are requesting. Only IPv4 addresses may be allocated through this operation. **(Choices: `ipv4`)** |
| `address` | <center>`str`</center> | <center>Optional</center> | The IP address to delete. **(Conflicts With: `linode_id`,`public`,`type`)** |

## Return Values

7 changes: 4 additions & 3 deletions examples/obj_static_site/roles/static_site/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
- name: Create an Object Storage bucket
amazon.aws.s3_bucket:
name: "{{ bucket_name }}"
s3_url: "https://{{ cluster_info.clusters[0].domain }}/"
endpoint_url: "https://{{ cluster_info.clusters[0].domain }}/"
aws_access_key: "{{ obj_key.key.access_key }}"
aws_secret_key: "{{ obj_key.key.secret_key }}"

state: present

- name: Upload the static site files
amazon.aws.aws_s3:
s3_url: "https://{{ cluster_info.clusters[0].domain }}/"
amazon.aws.s3_object:
endpoint_url: "https://{{ cluster_info.clusters[0].domain }}/"
aws_access_key: "{{ obj_key.key.access_key }}"
aws_secret_key: "{{ obj_key.key.secret_key }}"

Expand All @@ -29,6 +29,7 @@
src: "{{ item }}"
mode: put
permission: public-read
ceph: true
with_fileglob: 'roles/static_site/files/public/*.html'

- name: Configure the Object Storage Bucket as a website with index and error pages
Expand Down
25 changes: 25 additions & 0 deletions plugins/module_utils/doc_fragments/ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Documentation fragments for the ip module"""
specdoc_examples = ['''
- name: Allocate IP to Linode
linode.cloud.ip:
linode_id: 123
public: true
type: ipv4
state: present''']

result_ip_samples = ['''{
"address": "97.107.143.141",
"gateway": "97.107.143.1",
"linode_id": 123,
"prefix": 24,
"public": true,
"rdns": "test.example.org",
"region": "us-east",
"subnet_mask": "255.255.255.0",
"type": "ipv4",
"vpc_nat_1_1": {
"vpc_id": 242,
"subnet_id": 194,
"address": "139.144.244.36"
}
}''']
137 changes: 137 additions & 0 deletions plugins/modules/ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""This module allows users to allocate a new IPv4 Address on their accounts."""

from __future__ import absolute_import, division, print_function

from typing import Any, Optional

import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.ip as docs
from ansible_collections.linode.cloud.plugins.module_utils.linode_common import (
LinodeModuleBase,
)
from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import (
global_authors,
global_requirements,
)
from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import (
filter_null_values,
)
from ansible_specdoc.objects import FieldType, SpecDocMeta, SpecField

spec: dict = {
"linode_id": SpecField(
type=FieldType.integer,
description=[
"The ID of a Linode you have access to "
"that this address will be allocated to."
],
),
"public": SpecField(
type=FieldType.bool,
description=["Whether to create a public or private IPv4 address."],
),
"type": SpecField(
type=FieldType.string,
choices=["ipv4"],
description=[
"The type of address you are requesting. "
"Only IPv4 addresses may be allocated through this operation."
],
),
"address": SpecField(
type=FieldType.string,
description=["The IP address to delete."],
conflicts_with=["linode_id", "public", "type"],
),
"state": SpecField(
type=FieldType.string,
choices=["present", "absent"],
required=True,
description=["The state of this IP."],
),
}

SPECDOC_META = SpecDocMeta(
description=[
"Allocates a new IPv4 Address on your Account. "
"The Linode must be configured to support "
"additional addresses - "
"please Open a support ticket "
"requesting additional addresses before attempting allocation.",
],
requirements=global_requirements,
author=global_authors,
options=spec,
examples=docs.specdoc_examples,
return_values={},
)

DOCUMENTATION = r"""
"""
EXAMPLES = r"""
"""
RETURN = r"""
"""


class Module(LinodeModuleBase):
"""Module for allocating a new IP"""

def __init__(self) -> None:
self.module_arg_spec = SPECDOC_META.ansible_spec
self.results = {
"changed": False,
"actions": [],
"ip": None,
}
super().__init__(
module_arg_spec=self.module_arg_spec,
required_together=[
("linode_id", "public", "type"),
],
)

def _handle_present(self) -> None:
params = filter_null_values(self.module.params)
linode_id = params.get("linode_id")
public = params.get("public")

try:
ip = self.client.networking.ip_allocate(linode_id, public)
self.register_action(
f"IP allocation to Linode {linode_id} completed."
)
except Exception as exc:
self.fail(msg=f"failed to allocate IP to Linode {linode_id}: {exc}")

self.results["ip"] = ip._raw_json

def _handle_absent(self) -> None:
# TODO: Implement deleting IP once it's available in python-sdk.
# Raise an error for now when user reaches deleting IP.
self.fail(
msg="failed to delete IP: IP deleting is currently not supported."
)

def exec_module(self, **kwargs: Any) -> Optional[dict]:
"""Entrypoint for IP module"""

state = kwargs.get("state")

if state == "absent":
self._handle_absent()
return self.results

self._handle_present()

return self.results


def main() -> None:
"""Constructs and calls the module"""
Module()


if __name__ == "__main__":
main()
Loading

0 comments on commit e5e7e9f

Please sign in to comment.