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

Add AWS AMI Resource and Data Source Support #94

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
74 changes: 74 additions & 0 deletions docs/data-sources/aws_ami.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "kion_aws_ami Data Source - terraform-provider-kion"
subcategory: ""
description: |-

---

# kion_aws_ami (Data Source)



## Example Usage

```terraform
data "kion_aws_ami" "example" {
filter {
name = "region"
values = ["us-west-2"]
}

filter {
name = "name"
values = ["^MyExampleAMI.*"] # Use regex if you want to match a pattern
regex = true
}
}

output "ami_list" {
value = data.kion_aws_ami.example.list
}

output "first_ami_id" {
value = data.kion_aws_ami.example.list[0].aws_ami_id
}

output "first_ami_name" {
value = data.kion_aws_ami.example.list[0].name
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `filter` (Block List) (see [below for nested schema](#nestedblock--filter))

### Read-Only

- `account_id` (Number) AWS account application ID where the AMI is stored.
- `aws_ami_id` (String) Image ID of the AMI from AWS.
- `description` (String) Description for the AMI in the application.
- `expires_at` (String) The expiration date and time of the AMI. This may be null.
- `id` (String) The ID of this resource.
- `name` (String) The name of the AMI.
- `owner_user_group_ids` (List of Number) List of group IDs who own the AMI.
- `owner_user_ids` (List of Number) List of user IDs who own the AMI.
- `region` (String) AWS region where the AMI exists.
- `sync_deprecation` (Boolean) Will sync the expiration date from the system into the AMI in AWS.
- `sync_tags` (Boolean) Will sync the AWS tags from the source AMI into all the accounts where the AMI is shared.
- `unavailable_in_aws` (Boolean) Indicates if the AMI is unavailable in AWS.

<a id="nestedblock--filter"></a>
### Nested Schema for `filter`

Required:

- `name` (String) The field name whose values you wish to filter by.
- `values` (List of String) The values of the field name you specified.

Optional:

- `regex` (Boolean) Dictates if the values provided should be treated as regular expressions.
71 changes: 71 additions & 0 deletions docs/resources/aws_ami.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "kion_aws_ami Resource - terraform-provider-kion"
subcategory: ""
description: |-

---

# kion_aws_ami (Resource)



## Example Usage

```terraform
resource "kion_aws_ami" "example" {
account_id = 1 # Required: AWS account application ID where the AMI is stored
aws_ami_id = "ami-123456" # Required: Image ID of the AMI from AWS
description = "Gold image for RHEL 7.5." # Optional: Description for the AMI in the application
expiration_alert_number = 1 # Optional: The amount of time before the expiration alert is shown
expiration_alert_unit = "days" # Optional: The unit of time for the expiration alert (e.g., 'days', 'hours')
expiration_notify = true # Optional: Will notify the owners that the shared AMI has expired
expiration_warning_number = 1 # Optional: The amount of time before the expiration warning is sent
expiration_warning_unit = "days" # Optional: The unit of time for the expiration warning (e.g., 'days', 'hours')
expires_at = "2024-12-31T22:10:41.406Z" # Optional: Set an expiration date
name = "rhel-7-5-20180213" # Required: The name of the AMI
owner_user_group_ids = [1, 2] # Optional: List of group IDs who will own the AMI
owner_user_ids = [1, 2] # Optional: List of user IDs who will own the AMI
region = "us-east-1" # Required: AWS region where the AMI exists
sync_deprecation = true # Optional: Will sync the expiration date from the system into the AMI in AWS
sync_tags = true # Optional: Will sync the AWS tags from the source AMI into all the accounts where the AMI is shared
}

output "ami_id" {
value = kion_aws_ami.example.id
}

output "ami_name" {
value = kion_aws_ami.example.name
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `account_id` (Number) AWS account application ID where the AMI is stored.
- `aws_ami_id` (String) Image ID of the AMI from AWS.
- `name` (String) The name of the AMI.
- `region` (String) AWS region where the AMI exists.

### Optional

- `description` (String) Description for the AMI in the application.
- `expiration_alert_number` (Number) The amount of time before the expiration alert is shown.
- `expiration_alert_unit` (String) The unit of time for the expiration alert (e.g., 'days', 'hours'). This may be null.
- `expiration_notify` (Boolean) Will notify the owners that the shared AMI has expired.
- `expiration_warning_number` (Number) The amount of time before the expiration warning is sent.
- `expiration_warning_unit` (String) The unit of time for the expiration warning (e.g., 'days', 'hours'). This may be null.
- `expires_at` (String) The expiration date and time of the AMI. This may be null.
- `owner_user_group_ids` (List of Number) List of group IDs who will own the AMI. Required if no owner user IDs are listed.
- `owner_user_ids` (List of Number) List of user IDs who will own the AMI. Required if no owner group IDs are listed.
- `sync_deprecation` (Boolean) Will sync the expiration date from the system into the AMI in AWS.
- `sync_tags` (Boolean) Will sync the AWS tags from the source AMI into all the accounts where the AMI is shared.
- `unavailable_in_aws` (Boolean) Indicates if the AMI is unavailable in AWS.

### Read-Only

- `id` (String) The ID of this resource.
- `last_updated` (String)
24 changes: 24 additions & 0 deletions examples/data-sources/kion_aws_ami/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
data "kion_aws_ami" "example" {
filter {
name = "region"
values = ["us-west-2"]
}

filter {
name = "name"
values = ["^MyExampleAMI.*"] # Use regex if you want to match a pattern
regex = true
}
}

output "ami_list" {
value = data.kion_aws_ami.example.list
}

output "first_ami_id" {
value = data.kion_aws_ami.example.list[0].aws_ami_id
}

output "first_ami_name" {
value = data.kion_aws_ami.example.list[0].name
}
25 changes: 25 additions & 0 deletions examples/resources/kion_aws_ami/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "kion_aws_ami" "example" {
account_id = 1 # Required: AWS account application ID where the AMI is stored
aws_ami_id = "ami-123456" # Required: Image ID of the AMI from AWS
description = "Gold image for RHEL 7.5." # Optional: Description for the AMI in the application
expiration_alert_number = 1 # Optional: The amount of time before the expiration alert is shown
expiration_alert_unit = "days" # Optional: The unit of time for the expiration alert (e.g., 'days', 'hours')
expiration_notify = true # Optional: Will notify the owners that the shared AMI has expired
expiration_warning_number = 1 # Optional: The amount of time before the expiration warning is sent
expiration_warning_unit = "days" # Optional: The unit of time for the expiration warning (e.g., 'days', 'hours')
expires_at = "2024-12-31T22:10:41.406Z" # Optional: Set an expiration date
name = "rhel-7-5-20180213" # Required: The name of the AMI
owner_user_group_ids = [1, 2] # Optional: List of group IDs who will own the AMI
owner_user_ids = [1, 2] # Optional: List of user IDs who will own the AMI
region = "us-east-1" # Required: AWS region where the AMI exists
sync_deprecation = true # Optional: Will sync the expiration date from the system into the AMI in AWS
sync_tags = true # Optional: Will sync the AWS tags from the source AMI into all the accounts where the AMI is shared
}

output "ami_id" {
value = kion_aws_ami.example.id
}

output "ami_name" {
value = kion_aws_ami.example.name
}
161 changes: 161 additions & 0 deletions kion/data_source_aws_ami.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package kion

import (
"context"
"fmt"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
hc "github.com/kionsoftware/terraform-provider-kion/kion/internal/kionclient"
)

func dataSourceAwsAmi() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAwsAmiRead,
Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Description: "The field name whose values you wish to filter by.",
Type: schema.TypeString,
Required: true,
},
"regex": {
Description: "Dictates if the values provided should be treated as regular expressions.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"values": {
Description: "The values of the field name you specified.",
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"account_id": {
Type: schema.TypeInt,
Computed: true,
Description: "AWS account application ID where the AMI is stored.",
},
"aws_ami_id": {
Type: schema.TypeString,
Computed: true,
Description: "Image ID of the AMI from AWS.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description for the AMI in the application.",
},
"expires_at": {
Type: schema.TypeString,
Computed: true,
Description: "The expiration date and time of the AMI. This may be null.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the AMI.",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "AWS region where the AMI exists.",
},
"sync_deprecation": {
Type: schema.TypeBool,
Computed: true,
Description: "Will sync the expiration date from the system into the AMI in AWS.",
},
"sync_tags": {
Type: schema.TypeBool,
Computed: true,
Description: "Will sync the AWS tags from the source AMI into all the accounts where the AMI is shared.",
},
"unavailable_in_aws": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates if the AMI is unavailable in AWS.",
},
"owner_user_group_ids": {
Type: schema.TypeList,
Computed: true,
Description: "List of group IDs who own the AMI.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
"owner_user_ids": {
Type: schema.TypeList,
Computed: true,
Description: "List of user IDs who own the AMI.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}

func dataSourceAwsAmiRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hc.Client)

resp := new(hc.AmiListResponse)
err := client.GET("/v3/ami", resp)
if err != nil {
return hc.HandleError(fmt.Errorf("unable to read AWS AMI: %v", err))
}

f := hc.NewFilterable(d)

arr := make([]map[string]interface{}, 0)
for _, item := range resp.Data {
ami := item.Ami

data := make(map[string]interface{})
data["account_id"] = ami.AccountID
data["aws_ami_id"] = ami.AwsAmiID
data["description"] = ami.Description

if ami.ExpiresAt.Valid {
data["expires_at"] = ami.ExpiresAt.Time.Format(time.RFC3339)
} else {
data["expires_at"] = nil
}

data["id"] = ami.ID
data["name"] = ami.Name
data["region"] = ami.Region
data["sync_deprecation"] = ami.SyncDeprecation
data["sync_tags"] = ami.SyncTags
data["unavailable_in_aws"] = ami.UnavailableInAws
data["owner_user_groups"] = hc.InflateObjectWithID(item.OwnerUserGroups)
data["owner_users"] = hc.InflateObjectWithID(item.OwnerUsers)

match, err := f.Match(data)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to filter AWS AMI",
Detail: fmt.Sprintf("Error: %v\nItem: %v", err.Error(), "filter"),
})
return diags
} else if !match {
continue
}

arr = append(arr, data)
}

diags = append(diags, hc.SafeSet(d, "list", arr, "Unable to read AWS AMI")...)

// Always run.
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))

return diags
}
Loading