Skip to content

Commit

Permalink
fix(LH-70649): Add the cdFMC domain UUID to list of data returned by …
Browse files Browse the repository at this point in the history
…the cdfmc data source (#49)
  • Loading branch information
siddhuwarrier authored Sep 14, 2023
1 parent 55ad4f9 commit 5244a25
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func (c *Client) ReadTenantDetails(ctx context.Context) (*tenant.ReadTenantDetai
return tenant.ReadTenantDetails(ctx, c.client)
}

func (c *Client) ReadCloudFmc(ctx context.Context) (*device.ReadOutput, error) {
func (c *Client) ReadCloudFmcDevice(ctx context.Context) (*device.ReadOutput, error) {
return cloudfmc.Read(ctx, c.client, cloudfmc.NewReadInput())
}

func (c *Client) ReadCloudFmcSpecificDevice(ctx context.Context, inp cloudfmc.ReadSpecificInput) (*cloudfmc.ReadSpecificOutput, error) {
return cloudfmc.ReadSpecific(ctx, c.client, inp)
}
4 changes: 4 additions & 0 deletions provider/examples/data-sources/cdfmc/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ output "cdfmc_software_version" {

output "cdfmc_uid" {
value = data.cdo_cdfmc.current.id
}

output "cdfmc_domain_uuid" {
value = data.cdo_cdfmc.current.domain_uuid
}
21 changes: 17 additions & 4 deletions provider/internal/cdfmc/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

cdoClient "github.com/CiscoDevnet/terraform-provider-cdo/go-client"
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/device/cloudfmc"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -14,6 +15,7 @@ type DataSourceModel struct {
Uid types.String `tfsdk:"id"`
Hostname types.String `tfsdk:"hostname"`
SoftwareVersion types.String `tfsdk:"software_version"`
DomainUuid types.String `tfsdk:"domain_uuid"`
}

func NewDataSource() datasource.DataSource {
Expand Down Expand Up @@ -44,6 +46,10 @@ func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, r
MarkdownDescription: "Software version of the cdFMC.",
Computed: true,
},
"domain_uuid": schema.StringAttribute{
MarkdownDescription: "The domain UUID of the cdFMC.",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -77,14 +83,21 @@ func (d *DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp
return
}

cloudFmc, err := d.client.ReadCloudFmc(ctx)
cloudFmcDevice, err := d.client.ReadCloudFmcDevice(ctx)
if err != nil {
resp.Diagnostics.AddError("Failed to read cdFMC", err.Error())
return
}
planData.Uid = types.StringValue(cloudFmc.Uid)
planData.Hostname = types.StringValue(cloudFmc.Host)
planData.SoftwareVersion = types.StringValue(cloudFmc.SoftwareVersion)
cloudFmcSpecificDevice, cloudFmcSpecificDeviceErr := d.client.ReadCloudFmcSpecificDevice(ctx, cloudfmc.NewReadSpecificInput(cloudFmcDevice.Uid))
if cloudFmcSpecificDeviceErr != nil {
resp.Diagnostics.AddError("Failed to read cdFMC specific device", err.Error())
return
}

planData.Uid = types.StringValue(cloudFmcDevice.Uid)
planData.Hostname = types.StringValue(cloudFmcDevice.Host)
planData.DomainUuid = types.StringValue(cloudFmcSpecificDevice.DomainUid)
planData.SoftwareVersion = types.StringValue(cloudFmcDevice.SoftwareVersion)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &planData)...)
Expand Down
9 changes: 6 additions & 3 deletions provider/internal/cdfmc/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ var testCdFmc = struct {
Hostname string
Uid string
SoftwareVersion string
DomainUuid string
}{
Hostname: "terraform-provider-cdo.app.staging.cdo.cisco.com",
Uid: "ac12b246-ed93-4a09-bc8a-5c4708854a2f",
SoftwareVersion: "7.3.1-build 6035",
DomainUuid: "e276abec-e0f2-11e3-8169-6d9ed49b625f",
}

const testTenantTemplate = `
const testCdFmcTemplate = `
data "cdo_cdfmc" "test" {}`

var testTenantConfig = acctest.MustParseTemplate(testTenantTemplate, testCdFmc)
var testCdfmcConfig = acctest.MustParseTemplate(testCdFmcTemplate, testCdFmc)

func TestAccCdFmcDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand All @@ -29,11 +31,12 @@ func TestAccCdFmcDataSource(t *testing.T) {
Steps: []resource.TestStep{
// Read testing
{
Config: acctest.ProviderConfig() + testTenantConfig,
Config: acctest.ProviderConfig() + testCdfmcConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.cdo_cdfmc.test", "hostname", testCdFmc.Hostname),
resource.TestCheckResourceAttr("data.cdo_cdfmc.test", "id", testCdFmc.Uid),
resource.TestCheckResourceAttr("data.cdo_cdfmc.test", "software_version", testCdFmc.SoftwareVersion),
resource.TestCheckResourceAttr("data.cdo_cdfmc.test", "domain_uuid", testCdFmc.DomainUuid),
),
},
},
Expand Down

0 comments on commit 5244a25

Please sign in to comment.