Skip to content

Commit

Permalink
Merge pull request #1 from TeeVeeCorp/contenttype-environment-support
Browse files Browse the repository at this point in the history
Contenttype environment support
  • Loading branch information
jchenry authored Aug 11, 2024
2 parents e1f97e0 + 1a2bd4e commit a1cf31a
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 52 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Contentful
Copyright (c) 2017 Telly Inc.,Contentful

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 3 additions & 0 deletions contentful/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contentful

import (
"os"
"time"
)

var (
Expand All @@ -13,3 +14,5 @@ var (
// Terraform configuration values
logBoolean = os.Getenv("TF_LOG")
)

var consistencyBuffer time.Duration = 10 * time.Second
1 change: 1 addition & 0 deletions contentful/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contentful

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
contentful "github.com/labd/contentful-go"
Expand Down
26 changes: 26 additions & 0 deletions contentful/resource_contentful_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package contentful
import (
"context"
"errors"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/labd/contentful-go"
Expand Down Expand Up @@ -30,6 +32,11 @@ func resourceContentfulAPIKey() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"environment_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand All @@ -43,13 +50,19 @@ func resourceContentfulAPIKey() *schema.Resource {
}

func resourceCreateAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)

apiKey := &contentful.APIKey{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}

environmentID := d.Get("environment_id").(string)
if environmentID != "" {
apiKey.Environments = append(apiKey.Environments, environmentLink(environmentID))
}

err := client.APIKeys.Upsert(d.Get("space_id").(string), apiKey)
if err != nil {
return parseError(err)
Expand All @@ -65,6 +78,7 @@ func resourceCreateAPIKey(_ context.Context, d *schema.ResourceData, m interface
}

func resourceUpdateAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
apiKeyID := d.Id()
Expand Down Expand Up @@ -92,6 +106,7 @@ func resourceUpdateAPIKey(_ context.Context, d *schema.ResourceData, m interface
}

func resourceReadAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
apiKeyID := d.Id()
Expand All @@ -112,6 +127,7 @@ func resourceReadAPIKey(_ context.Context, d *schema.ResourceData, m interface{}
}

func resourceDeleteAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
apiKeyID := d.Id()
Expand Down Expand Up @@ -152,3 +168,13 @@ func setAPIKeyProperties(d *schema.ResourceData, apiKey *contentful.APIKey) erro

return nil
}

func environmentLink(environmentID string) contentful.Environments {
return contentful.Environments{
Sys: contentful.Sys{
Type: "Link",
LinkType: "Environment",
ID: environmentID,
},
}
}
7 changes: 6 additions & 1 deletion contentful/resource_contentful_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package contentful
import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/labd/contentful-go"
)
Expand Down Expand Up @@ -145,6 +146,7 @@ func resourceContentfulAsset() *schema.Resource {
}

func resourceCreateAsset(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)

fields := d.Get("fields").([]interface{})[0].(map[string]interface{})
Expand Down Expand Up @@ -233,6 +235,7 @@ func resourceCreateAsset(_ context.Context, d *schema.ResourceData, m interface{
}

func resourceUpdateAsset(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
assetID := d.Id()
Expand Down Expand Up @@ -357,6 +360,7 @@ func setAssetState(d *schema.ResourceData, m interface{}) (err error) {
}

func resourceReadAsset(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
assetID := d.Id()
Expand All @@ -377,6 +381,7 @@ func resourceReadAsset(_ context.Context, d *schema.ResourceData, m interface{})
}

func resourceDeleteAsset(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
time.Sleep(consistencyBuffer)
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
assetID := d.Id()
Expand Down
Loading

0 comments on commit a1cf31a

Please sign in to comment.