-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(LH-70474): Add tenant data source (#42)
This commit adds a new tenant data source that provides information on the currently logged in tenant.
- Loading branch information
1 parent
26e5281
commit a8cb2ac
Showing
12 changed files
with
316 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package tenant_test | ||
|
||
const ( | ||
baseUrl = "https://unittest.cdo.cisco.com" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tenant | ||
|
||
type TenantDetailsDetails struct { | ||
TenantName string `json:"TenantName"` | ||
TenantOrganizationName string `json:"TenantOrganizationName"` | ||
TenantPayType string `json:"TenantPayType"` | ||
TenantUid string `json:"TenantUid"` | ||
} | ||
|
||
type UserAuthentication struct { | ||
Details TenantDetailsDetails `json:"details"` | ||
} | ||
|
||
type ReadTenantDetailsOutput struct { | ||
UserAuthentication UserAuthentication `json:"userAuthentication"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tenant | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http" | ||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/url" | ||
) | ||
|
||
func ReadTenantDetails(ctx context.Context, client http.Client) (*ReadTenantDetailsOutput, error) { | ||
client.Logger.Println("Get tenant details for currently connected client") | ||
|
||
req := NewReadTenantDetailsRequest(ctx, client) | ||
|
||
var outp ReadTenantDetailsOutput | ||
if err := req.Send(&outp); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &outp, nil | ||
} | ||
|
||
func NewReadTenantDetailsRequest(ctx context.Context, client http.Client) *http.Request { | ||
url := url.ReadTenantDetails(client.BaseUrl()) | ||
return client.NewGet(ctx, url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package tenant_test | ||
|
||
import ( | ||
"context" | ||
netHttp "net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http" | ||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/tenant" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadTenantDetails(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
t.Run("Should get current tenant details", func(t *testing.T) { | ||
httpmock.Reset() | ||
expected := tenant.ReadTenantDetailsOutput{ | ||
UserAuthentication: tenant.UserAuthentication{ | ||
Details: tenant.TenantDetailsDetails{ | ||
TenantUid: "111-111-111-111", | ||
TenantName: "sample-tenant-name", | ||
TenantOrganizationName: "sample-org-name", | ||
TenantPayType: "NOT_PAYING", | ||
}, | ||
}, | ||
} | ||
httpmock.RegisterResponder( | ||
netHttp.MethodGet, | ||
"/anubis/rest/v1/oauth/check_token", | ||
httpmock.NewJsonResponderOrPanic(200, expected), | ||
) | ||
|
||
actual, err := tenant.ReadTenantDetails(context.Background(), *http.MustNewWithConfig(baseUrl, "valid token", 0, 0, time.Minute)) | ||
assert.NotNil(t, actual, "Read output should not be nil") | ||
assert.Equal(t, *actual, expected) | ||
assert.Nil(t, err, "error should be nil") | ||
}) | ||
|
||
t.Run("should error if getting current tenant details fails", func(t *testing.T) { | ||
httpmock.Reset() | ||
httpmock.RegisterResponder( | ||
netHttp.MethodGet, | ||
"/anubis/rest/v1/oauth/check_token", | ||
httpmock.NewJsonResponderOrPanic(500, nil), | ||
) | ||
|
||
actual, err := tenant.ReadTenantDetails(context.Background(), *http.MustNewWithConfig(baseUrl, "valid token", 0, 0, time.Minute)) | ||
assert.Nil(t, actual, "Read output should be nil") | ||
assert.NotNil(t, err, "error should not be nil") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cdo_tenant Data Source - cdo" | ||
subcategory: "" | ||
description: |- | ||
Use this data source to get information on the tenant upon which the Terraform provider is performing operations. | ||
--- | ||
|
||
# cdo_tenant (Data Source) | ||
|
||
Use this data source to get information on the tenant upon which the Terraform provider is performing operations. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `human_readable_name` (String) Human-readable name of the tenant as displayed on the CDO UI (if different from the tenant name). | ||
- `id` (String) Universally unique identifier for the tenant. | ||
- `name` (String) Name of the tenant. | ||
- `subscription_type` (String) The type of CDO subscription used on this tenant. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
terraform { | ||
required_providers { | ||
cdo = { | ||
source = "hashicorp.com/CiscoDevnet/cdo" | ||
} | ||
} | ||
} | ||
|
||
provider "cdo" { | ||
base_url = "<https://www.defenseorchestrator.com|https://www.defenseorchestrator.eu|https://apj.cdo.cisco.com>" | ||
api_token = "<replace-with-api-token-generated-from-cdo>" | ||
} | ||
|
||
data "cdo_tenant" "current" { | ||
} | ||
|
||
output "current_tenant_uid" { | ||
value = data.cdo_tenant.current.id | ||
} | ||
|
||
output "current_tenant_name" { | ||
value = data.cdo_tenant.current.name | ||
} | ||
|
||
output "current_tenant_human_readable_name" { | ||
value = data.cdo_tenant.current.human_readable_name | ||
} | ||
|
||
output "current_tenant_subscription_type" { | ||
value = data.cdo_tenant.current.subscription_type | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package tenant | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
cdoClient "github.com/CiscoDevnet/terraform-provider-cdo/go-client" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
type DataSourceModel struct { | ||
Uid types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
HumanReadableName types.String `tfsdk:"human_readable_name"` | ||
SubscriptionType types.String `tfsdk:"subscription_type"` | ||
} | ||
|
||
func NewDataSource() datasource.DataSource { | ||
return &DataSource{} | ||
} | ||
|
||
type DataSource struct { | ||
client *cdoClient.Client | ||
} | ||
|
||
func (d *DataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_tenant" | ||
} | ||
|
||
func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Use this data source to get information on the tenant upon which the Terraform provider is performing operations.", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
MarkdownDescription: "Universally unique identifier for the tenant.", | ||
Computed: true, | ||
}, | ||
"name": schema.StringAttribute{ | ||
MarkdownDescription: "Name of the tenant.", | ||
Computed: true, | ||
}, | ||
"human_readable_name": schema.StringAttribute{ | ||
MarkdownDescription: "Human-readable name of the tenant as displayed on the CDO UI (if different from the tenant name).", | ||
Computed: true, | ||
}, | ||
"subscription_type": schema.StringAttribute{ | ||
MarkdownDescription: "The type of CDO subscription used on this tenant.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *DataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*cdoClient.Client) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *cdoClient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
|
||
var planData DataSourceModel | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &planData)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
res, err := d.client.ReadTenantDetails(ctx) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Failed to read tenant", err.Error()) | ||
return | ||
} | ||
|
||
planData.Uid = types.StringValue(res.UserAuthentication.Details.TenantUid) | ||
planData.Name = types.StringValue(res.UserAuthentication.Details.TenantName) | ||
planData.HumanReadableName = types.StringValue(res.UserAuthentication.Details.TenantOrganizationName) | ||
planData.SubscriptionType = types.StringValue(res.UserAuthentication.Details.TenantPayType) | ||
tflog.Debug(ctx, fmt.Sprintf("Read tenant details %+v", planData)) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &planData)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package tenant_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/CiscoDevnet/terraform-provider-cdo/internal/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
var testTenant = struct { | ||
Name string | ||
Uid string | ||
HumanReadableName string | ||
SubscriptionType string | ||
}{ | ||
Name: "CDO_terraform-provider-cdo", | ||
Uid: "ae98d25f-1089-4286-a3c5-505dcb4431a2", | ||
HumanReadableName: "terraform-provider-cdo", | ||
SubscriptionType: "INTERNAL", | ||
} | ||
|
||
const testTenantTemplate = ` | ||
data "cdo_tenant" "test" {}` | ||
|
||
var testTenantConfig = acctest.MustParseTemplate(testTenantTemplate, testTenant) | ||
|
||
func TestAccTenantDataSource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: acctest.PreCheckFunc(t), | ||
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
Config: acctest.ProviderConfig() + testTenantConfig, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.cdo_tenant.test", "name", testTenant.Name), | ||
resource.TestCheckResourceAttr("data.cdo_tenant.test", "id", testTenant.Uid), | ||
resource.TestCheckResourceAttr("data.cdo_tenant.test", "human_readable_name", testTenant.HumanReadableName), | ||
resource.TestCheckResourceAttr("data.cdo_tenant.test", "subscription_type", testTenant.SubscriptionType), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |