Skip to content

Commit

Permalink
fix Optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
ludaciti committed Feb 18, 2022
1 parent 761341d commit 7663291
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TEST?=$$(go list ./... | grep -v 'vendor')
NAME=costradar
BINARY=terraform-provider-${NAME}_${VERSION}
VERSION=0.1.12
VERSION=0.1.13
HOSTNAME=localhost
NAMESPACE=local
GOARCH = amd64
Expand Down
66 changes: 47 additions & 19 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,53 @@ resource "costradar_cloudtrail_subscription" "test" {
]
}

resource "costradar_aws_account" "test" {
account_id = "123:123"
alias = "Alias"
access_config {
reader_mode = "direct"
}
owners = [
"email1@gmail.com",
"email2@gmail.com"
]
tags = {
name = "test"
tenant = "costradar"
}
}

resource "costradar_workload" "test" {
name = "Terraform Test Workload"
description = "test"
owners = [
"test@email.com"
]
tags = {
"name": "Daniel"
}
}

resource "costradar_user" "test" {
email = "terraform@gmail.com"
name = "Daniel"
initials = "D"
icon_url = "test"
tags = {
name = "test"
tenant = "costradar"
}
}

resource "costradar_team" "test" {
name = "Terraform Test Team"
description = "test"
tags = {
name = "test"
tenant = "costradar"
}
}

resource "costradar_identity_resolver" "this" {
lambda_arn = "123:xxx:yyy"
access_config {
Expand Down Expand Up @@ -80,11 +127,6 @@ output "cloudtrail_subscription" {
output "test" {
value = coalesce("", null, "123")
}
resource "costradar_workload" "test" {
name = "Terraform Test Workload"
owners = []
tags = {}
}

resource "costradar_workload_resource_set" "test" {
workload_id = costradar_workload.test.id
Expand All @@ -100,10 +142,6 @@ resource "costradar_workload_resource_set" "test" {
}
}

resource "costradar_team" "test" {
name = "Terraform Test Team"
}

resource "costradar_team_member_set" "test" {
team_id = costradar_team.test.id

Expand All @@ -116,16 +154,6 @@ resource "costradar_team_member_set" "test" {
}
}

resource "costradar_user" "test" {
email = "terraform@gmail.com"
name = "Daniel"
initials = "D"

tags = {
name = "test"
tenant = "costradar"
}
}

resource "costradar_user_identity_set" "test" {
user_id = costradar_user.test.id
Expand Down
13 changes: 10 additions & 3 deletions internal/provider/resource_aws_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ func awsAccountToResourceData(d *schema.ResourceData, a Account) {
d.Set("account_id", a.AccountId)
d.Set("alias", a.Alias)
d.Set("access_config", accessConfigList)
d.Set("owners", a.Owners)
d.Set("tags", a.Tags)
if nilIfEmpty(a.Owners) != nil {
d.Set("owners", a.Owners)
}
if nilIfEmpty(a.Tags) != nil {
d.Set("tags", a.Tags)
}
//d.Set("owners", a.Owners)
//d.Set("tags", a.Tags)
}

func resourceAwsAccountRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -142,8 +148,9 @@ func resourceAwsAccountCreate(ctx context.Context, d *schema.ResourceData, m int
if err != nil {
return diag.FromErr(err)
}
d.SetId(a.Payload.ID)

resourceAwsAccountRead(ctx, d, m)
d.SetId(a.Payload.ID)
return diags
}

Expand Down
16 changes: 10 additions & 6 deletions internal/provider/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ func resourceTeam() *schema.Resource {
}

func TeamFromResourceData(d *schema.ResourceData) Team {
Team := Team{
team := Team{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Tags: d.Get("tags").(map[string]interface{}),
}
return Team
return team
}

func TeamToResourceData(d *schema.ResourceData, t Team) {
d.Set("name", t.Name)
d.Set("description", t.Description)
d.Set("tags", t.Tags)
if nilIfEmpty(t.Description) != nil {
d.Set("description", t.Description)
}
if nilIfEmpty(t.Tags) != nil {
d.Set("tags", t.Tags)
}
}

func resourceTeamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -82,9 +86,9 @@ func resourceTeamCreate(ctx context.Context, d *schema.ResourceData, m interface
if err != nil {
return diag.FromErr(err)
}
d.SetId(w.Payload.ID)
resourceTeamRead(ctx, d, m)

resourceTeamRead(ctx, d, m)
d.SetId(w.Payload.ID)
return diags
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccTeam(t *testing.T) {
Config: testAccTeamTF(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "Terraform Test Team"),
resource.TestCheckResourceAttr(resourceName, "description", ""),
//resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "tags.name", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.tenant", "costradar"),
),
Expand Down
12 changes: 8 additions & 4 deletions internal/provider/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ func UserFromResourceData(d *schema.ResourceData) User {
func UserToResourceData(d *schema.ResourceData, u User) {
d.Set("name", u.Name)
d.Set("initials", u.Initials)
d.Set("icon_url", u.IconUrl)
d.Set("email", u.Email)
d.Set("tags", u.Tags)
if nilIfEmpty(u.IconUrl) != nil {
d.Set("icon_url", u.IconUrl)
}
if nilIfEmpty(u.Tags) != nil {
d.Set("tags", u.Tags)
}
}

func resourceUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -95,9 +99,9 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, m interface
if err != nil {
return diag.FromErr(err)
}
d.SetId(w.Payload.ID)
resourceUserRead(ctx, d, m)

resourceUserRead(ctx, d, m)
d.SetId(w.Payload.ID)
return diags
}

Expand Down
16 changes: 11 additions & 5 deletions internal/provider/resource_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ func workloadFromResourceData(d *schema.ResourceData) Workload {

func workloadToResourceData(d *schema.ResourceData, w Workload) {
d.Set("name", w.Name)
d.Set("description", w.Description)
d.Set("owners", w.Owners)
d.Set("tags", w.Tags)
if nilIfEmpty(w.Owners) != nil {
d.Set("owners", w.Owners)
}
if nilIfEmpty(w.Tags) != nil {
d.Set("tags", w.Tags)
}
if nilIfEmpty(w.Description) != nil {
d.Set("description", w.Description)
}
}

func resourceWorkloadRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -91,9 +97,9 @@ func resourceWorkloadCreate(ctx context.Context, d *schema.ResourceData, m inter
if err != nil {
return diag.FromErr(err)
}
d.SetId(w.Payload.ID)
resourceWorkloadRead(ctx, d, m)

resourceWorkloadRead(ctx, d, m)
d.SetId(w.Payload.ID)
return diags
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccWorkload(t *testing.T) {
Config: testAccWorkloadTF(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "Terraform Test Workload"),
resource.TestCheckResourceAttr(resourceName, "description", ""),
//resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "owners.0", "email1@gmail.com"),
resource.TestCheckResourceAttr(resourceName, "owners.1", "email2@gmail.com"),
resource.TestCheckResourceAttr(resourceName, "tags.name", "test"),
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/oklog/ulid"
"github.com/tidwall/gjson"
"math/rand"
"reflect"
"time"
)

Expand All @@ -29,3 +30,23 @@ func getUniqueId() string {
id := ulid.MustNew(ulid.Timestamp(t), entropy)
return id.String()
}

func nilIfEmpty(v interface{}) interface{} {
switch v := v.(type) {
case string:
if v == "" {
return nil
} else {
return v
}
case []interface{}, map[string]interface{}:
s := reflect.ValueOf(v)
if s.Len() == 0 {
return nil
} else {
return v
}
default:
return ""
}
}

0 comments on commit 7663291

Please sign in to comment.