Skip to content
Open
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/confluentinc/terraform-provider-confluent
go 1.22.7

require (
github.com/confluentinc/ccloud-sdk-go-v2-internal/parent v0.1.0
github.com/confluentinc/ccloud-sdk-go-v2/apikeys v0.4.0
github.com/confluentinc/ccloud-sdk-go-v2/byok v0.0.2
github.com/confluentinc/ccloud-sdk-go-v2/certificate-authority v0.0.2
github.com/confluentinc/ccloud-sdk-go-v2/cam v0.3.0
github.com/confluentinc/ccloud-sdk-go-v2/certificate-authority v0.0.2
github.com/confluentinc/ccloud-sdk-go-v2/cmk v0.21.0
github.com/confluentinc/ccloud-sdk-go-v2/connect v0.7.0
github.com/confluentinc/ccloud-sdk-go-v2/connect-custom-plugin v0.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/confluentinc/ccloud-sdk-go-v2-internal/parent v0.1.0 h1:VKRni4pO2zguteFrnfkKVrBhxU2HMIMH9MoiVdRbajs=
github.com/confluentinc/ccloud-sdk-go-v2-internal/parent v0.1.0/go.mod h1:GXiUIE5mffWCMsIrznGrdrtRuvYC7JfU1gIyJp5A78w=
github.com/confluentinc/ccloud-sdk-go-v2/apikeys v0.4.0 h1:8fWyLwMuy8ec0MVF5Avd54UvbIxhDFhZzanHBVwgxdw=
github.com/confluentinc/ccloud-sdk-go-v2/apikeys v0.4.0/go.mod h1:wNa9Qg2e2v/+PQsUyKh+qB22hhLkPR6Ahy6rP+1jmGI=
github.com/confluentinc/ccloud-sdk-go-v2/byok v0.0.2 h1:BV/dPTFVovJPylrcr9lcSmukCVxBxyUeHBr6hp7zUNk=
github.com/confluentinc/ccloud-sdk-go-v2/byok v0.0.2/go.mod h1:MMtRTfg1g32bQrRwfqvGpe+grS5pQzeq9V+L5GKydV4=
github.com/confluentinc/ccloud-sdk-go-v2/cam v0.2.0 h1:kn54sB+/iDIyjxB23KEjkJs4t5AkL/C1eVj1zFhhueg=
github.com/confluentinc/ccloud-sdk-go-v2/cam v0.2.0/go.mod h1:C8wyHqXaX0KtA9aKg4mLwQdsaQV1O6uGrXXlLx/Lonw=
github.com/confluentinc/ccloud-sdk-go-v2/cam v0.3.0 h1:9MMm9VeJ8ZVg9A6ofplPPvLZH4OGP56PtrJ/WP18EtY=
github.com/confluentinc/ccloud-sdk-go-v2/cam v0.3.0/go.mod h1:vderFceIQXBEdhkulkwTZ0SPxHz4yZ3uDMne/FA4dJ8=
github.com/confluentinc/ccloud-sdk-go-v2/certificate-authority v0.0.2 h1:stsiO1JIRX6ITdw4DCsidQ0w7uhsyKDsYXwzxvi14GI=
Expand Down
92 changes: 92 additions & 0 deletions internal/provider/data_source_parent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2021 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider

import (
"context"
"encoding/json"
"fmt"
parent "github.com/confluentinc/ccloud-sdk-go-v2-internal/parent/v2"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net/http"
)

const paramInvitationRestrictionsEnabled = "invitation_restrictions_enabled"

func parentDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: parentDataSourceRead,
Schema: map[string]*schema.Schema{
paramId: {
Type: schema.TypeString,
Description: "The id of the Parent",
Required: true,
},
paramInvitationRestrictionsEnabled: {
Type: schema.TypeBool,
Description: "Controls whether the Parent has domain-based invitation restrictions enabled.",
Computed: true,
},
paramResourceName: {
Type: schema.TypeString,
Description: "The Confluent Resource Name of the Parent.",
Computed: true,
},
},
}
}

func parentDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
parentId := d.Get(paramId).(string)
if parentId == "" {
return diag.Errorf("error reading Parent: Parent id is missing")
}

tflog.Debug(ctx, fmt.Sprintf("Reading Parent %q=%q", paramId, parentId), map[string]interface{}{byokKeyLoggingKey: parentId})

c := meta.(*Client)
key, _, err := executeParentRead(c.byokApiContext(ctx), c, parentId)
if err != nil {
return diag.Errorf("error reading Parent %q: %s", parentId, createDescriptiveError(err))
}
keyJson, err := json.Marshal(key)
if err != nil {
return diag.Errorf("error reading Parent %q: error marshaling %#v to json: %s", parentId, key, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Parent %q: %s", parentId, keyJson), map[string]interface{}{byokKeyLoggingKey: parentId})

if _, err := setParentAttributes(d, key); err != nil {
return diag.FromErr(createDescriptiveError(err))
}
return nil
}

func executeParentRead(ctx context.Context, c *Client, id string) (parent.IamV2Parent, *http.Response, error) {
req := c.parentClient.ParentsIamV2Api.GetIamV2Parent(c.parentApiContext(ctx), id)
return req.Execute()
}

func setParentAttributes(d *schema.ResourceData, parentOrganization parent.IamV2Parent) (*schema.ResourceData, error) {
if err := d.Set(paramInvitationRestrictionsEnabled, parentOrganization.GetInvitationRestrictionsEnabled()); err != nil {
return nil, err
}
if err := d.Set(paramResourceName, parentOrganization.Metadata.GetResourceName()); err != nil {
return nil, err
}
d.SetId(parentOrganization.GetId())
return d, nil
}
62 changes: 62 additions & 0 deletions internal/provider/data_source_parent_organization_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider

import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func parentOrganizationLinkDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: parentOrganizationLinkDataSourceRead,
Schema: map[string]*schema.Schema{
paramId: {
Type: schema.TypeString,
Description: "The id of the Parent Organization Link",
Required: true,
},
paramParent: optionalIdBlockSchema(),
paramOrganization: optionalIdBlockSchema(),
},
}
}

func parentOrganizationLinkDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
parentOrganizationLinkId := d.Get(paramId).(string)

tflog.Debug(ctx, fmt.Sprintf("Reading Parent Organization Link %q=%q", paramId, parentOrganizationLinkId), map[string]interface{}{parentOrganizationLinkLoggingKey: parentOrganizationLinkId})

c := meta.(*Client)
parentOrganizationLink, _, err := executeParentOrganizationLinkRead(c.parentApiContext(ctx), c, parentOrganizationLinkId)
if err != nil {
return diag.Errorf("error reading Parent Organization Link %q: %s", parentOrganizationLinkId, createDescriptiveError(err))
}
parentOrganizationLinkJson, err := json.Marshal(parentOrganizationLink)
if err != nil {
return diag.Errorf("error reading Parent Organization Link %q: error marshaling %#v to json: %s", parentOrganizationLinkId, parentOrganizationLink, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Parent Organization Link %q: %s", parentOrganizationLinkId, parentOrganizationLinkJson), map[string]interface{}{parentOrganizationLinkLoggingKey: parentOrganizationLinkId})

if _, err := setParentOrganizationLinkAttributes(d, parentOrganizationLink); err != nil {
return diag.FromErr(createDescriptiveError(err))
}

return nil
}
96 changes: 96 additions & 0 deletions internal/provider/data_source_parent_organization_link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2022 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider

import (
"context"
"fmt"
"github.com/walkerus/go-wiremock"
"io/ioutil"
"net/http"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
parentOrganizationLinkDataSourceScenarioName = "confluent_parent_organization_link Data Source Lifecycle"
parentOrganizationLinkDataSourceLabel = "test_parent_organization_link_data_source_label"

expectedParentOrganizationLinkId = "78acf398-ef5d-4373-ac40-e2f99ca8f991"
expectedParentOrganizationLinkParentId = "98c3e8af-c392-418a-b87e-af33c7853b1f"
expectedParentOrganizationLinkOrgId = "085d3340-aef4-41e7-b537-9804d34e18fc"
)

var fullParentOrganizationLinkDataSourceLabel = fmt.Sprintf("data.confluent_parent_organization_link.%s", parentOrganizationLinkDataSourceLabel)

func TestAccDataSourceParentOrganizationLink(t *testing.T) {
ctx := context.Background()

wiremockContainer, err := setupWiremock(ctx)
if err != nil {
t.Fatal(err)
}
defer wiremockContainer.Terminate(ctx)

mockServerUrl := wiremockContainer.URI
wiremockClient := wiremock.NewClient(mockServerUrl)
// nolint:errcheck
defer wiremockClient.Reset()

// nolint:errcheck
defer wiremockClient.ResetAllScenarios()

readParentOrganizationLinkResponse, _ := ioutil.ReadFile("../testdata/parent_organization_link/read_created_link.json")
_ = wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/iam/v2/parent-organization-links/78acf398-ef5d-4373-ac40-e2f99ca8f991")).
InScenario(parentOrganizationLinkDataSourceScenarioName).
WhenScenarioStateIs(wiremock.ScenarioStateStarted).
WillReturn(
string(readParentOrganizationLinkResponse),
contentTypeJSONHeader,
http.StatusOK,
))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
// https://www.terraform.io/docs/extend/testing/acceptance-tests/teststep.html
// https://www.terraform.io/docs/extend/best-practices/testing.html#built-in-patterns
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceParentOrganizationLinkConfig(mockServerUrl),
Check: resource.ComposeTestCheckFunc(
testAccCheckParentOrganizationLinkExists(fullParentOrganizationLinkDataSourceLabel),
resource.TestCheckResourceAttr(fullParentOrganizationLinkDataSourceLabel, "id", expectedParentOrganizationLinkId),
resource.TestCheckResourceAttr(fullParentOrganizationLinkDataSourceLabel, "parent.#", "1"),
resource.TestCheckResourceAttr(fullParentOrganizationLinkDataSourceLabel, "parent.0.id", expectedParentOrganizationLinkParentId),
resource.TestCheckResourceAttr(fullParentOrganizationLinkDataSourceLabel, "organization.#", "1"),
resource.TestCheckResourceAttr(fullParentOrganizationLinkDataSourceLabel, "organization.0.id", expectedParentOrganizationLinkOrgId),
),
},
},
})
}

func testAccCheckDataSourceParentOrganizationLinkConfig(confluentCloudBaseUrl string) string {
return fmt.Sprintf(`
provider "confluent" {
endpoint = "%s"
}
data "confluent_parent_organization_link" "%s" {
id = "78acf398-ef5d-4373-ac40-e2f99ca8f991"
}
`, confluentCloudBaseUrl, parentOrganizationLinkDataSourceLabel)
}
110 changes: 110 additions & 0 deletions internal/provider/data_source_parent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2021 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/walkerus/go-wiremock"
"io/ioutil"
"net/http"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
parentDataSourceScenarioName = "confluent_parent Data Source Lifecycle"
parentDataSourceLabel = "test_parent_data_source_label"

expectedParentResourceName = "crn://confluent.cloud/parent=1111aaaa-11aa-11aa-11aa-111111aaaaaa"
expectedParentId = "1111aaaa-11aa-11aa-11aa-111111aaaaaa"
)

var fullParentDataSourceLabel = fmt.Sprintf("data.confluent_parent.%s", parentDataSourceLabel)

func TestAccDataSourceParent(t *testing.T) {
ctx := context.Background()

wiremockContainer, err := setupWiremock(ctx)
if err != nil {
t.Fatal(err)
}
defer wiremockContainer.Terminate(ctx)

mockServerUrl := wiremockContainer.URI
wiremockClient := wiremock.NewClient(mockServerUrl)
// nolint:errcheck
defer wiremockClient.Reset()

// nolint:errcheck
defer wiremockClient.ResetAllScenarios()

readParentResponse, _ := ioutil.ReadFile("../testdata/parent/read_parent.json")
_ = wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/iam/v2/parent/1111aaaa-11aa-11aa-11aa-111111aaaaaa")).
InScenario(parentDataSourceScenarioName).
WhenScenarioStateIs(wiremock.ScenarioStateStarted).
WillReturn(
string(readParentResponse),
contentTypeJSONHeader,
http.StatusOK,
))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
// https://www.terraform.io/docs/extend/testing/acceptance-tests/teststep.html
// https://www.terraform.io/docs/extend/best-practices/testing.html#built-in-patterns
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceParentConfig(mockServerUrl),
Check: resource.ComposeTestCheckFunc(
testAccCheckParentExists(fullParentDataSourceLabel),
resource.TestCheckResourceAttr(fullParentDataSourceLabel, "id", expectedParentId),
resource.TestCheckResourceAttr(fullParentDataSourceLabel, "resource_name", expectedParentResourceName),
resource.TestCheckResourceAttr(fullParentDataSourceLabel, "invitation_restrictions_enabled", "true"),
),
},
},
})
}

func testAccCheckDataSourceParentConfig(confluentCloudBaseUrl string) string {
return fmt.Sprintf(`
provider "confluent" {
endpoint = "%s"
}
data "confluent_parent" "%s" {
id = "1111aaaa-11aa-11aa-11aa-111111aaaaaa"
}
`, confluentCloudBaseUrl, parentDataSourceLabel)
}

func testAccCheckParentExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

if !ok {
return fmt.Errorf("%s parent has not been found", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("ID has not been set for %s parent", n)
}

return nil
}
}
Loading