-
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-86970): add Terraform resource to add an existing tenant to M…
…SP portal using API token (#153) * feat(lh-86970): add Terraform resource to add an existing tenant to MSP portal using API token * fix(lh-86970): address Tal's comments and improve documentation * test(lh-86970): add more tests, make acceptance tests run temporarily on branch * revert this before merging * fix(lh-86970): add ADDED_MSP_MANAGED_TENANT_API_TOKEN to the env for acceptance tests * I hate Github * Stop running acceptance tests on branch
- Loading branch information
1 parent
688b711
commit 4364675
Showing
19 changed files
with
376 additions
and
24 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
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 @@ | ||
package tenants | ||
|
||
import ( | ||
"context" | ||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http" | ||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/url" | ||
) | ||
|
||
func AddExistingTenantUsingApiToken(ctx context.Context, client http.Client, addInp MspAddExistingTenantInput) (*MspTenantOutput, *CreateError) { | ||
client.Logger.Println("Adding existing tenant to MSp portal...") | ||
addUrl := url.AddExistingTenantToMspManagedTenant(client.BaseUrl()) | ||
|
||
req := client.NewPost(ctx, addUrl, addInp) | ||
|
||
var createOutp MspManagedTenantStatusInfo | ||
if err := req.Send(&createOutp); err != nil { | ||
return nil, &CreateError{Err: err} | ||
} | ||
|
||
client.Logger.Printf("Added existing tenant %s to MSP portal using API token...", createOutp.MspManagedTenant.Name) | ||
|
||
return &createOutp.MspManagedTenant, 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,66 @@ | ||
package tenants_test | ||
|
||
import ( | ||
"context" | ||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/internal/http" | ||
"github.com/CiscoDevnet/terraform-provider-cdo/go-client/msp/tenants" | ||
"github.com/google/uuid" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
netHttp "net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestAdd(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
t.Run("successfully add tenant using API token", func(t *testing.T) { | ||
httpmock.Reset() | ||
apiToken := "fake-jwt-token" | ||
addInp := tenants.MspAddExistingTenantInput{ | ||
ApiToken: apiToken, | ||
} | ||
expectedResponse := tenants.MspManagedTenantStatusInfo{ | ||
Status: "OK", | ||
MspManagedTenant: tenants.MspTenantOutput{ | ||
Uid: uuid.New().String(), | ||
Name: "example-name", | ||
DisplayName: "Human readable name", | ||
Region: "STAGING", | ||
}, | ||
} | ||
|
||
httpmock.RegisterResponder( | ||
netHttp.MethodPost, | ||
"/api/rest/v1/msp/tenants", | ||
httpmock.NewJsonResponderOrPanic(201, expectedResponse), | ||
) | ||
|
||
actual, err := tenants.AddExistingTenantUsingApiToken(context.Background(), *http.MustNewWithConfig(baseUrl, "valid_token", 0, 0, time.Minute), addInp) | ||
|
||
assert.NotNil(t, actual, "Response for added tenant should have not been nil") | ||
assert.Nil(t, err, "Add tenant operation should have not been an error") | ||
assert.Equal(t, expectedResponse.MspManagedTenant, *actual, "Add tenant operation should have returned the value of the added tenant") | ||
}) | ||
|
||
t.Run("fail to add tenant using API token", func(t *testing.T) { | ||
httpmock.Reset() | ||
apiToken := "fake-jwt-token" | ||
addInp := tenants.MspAddExistingTenantInput{ | ||
ApiToken: apiToken, | ||
} | ||
|
||
httpmock.RegisterResponder( | ||
netHttp.MethodPost, | ||
"/api/rest/v1/msp/tenants", | ||
httpmock.NewJsonResponderOrPanic(400, nil), | ||
) | ||
|
||
actual, err := tenants.AddExistingTenantUsingApiToken(context.Background(), *http.MustNewWithConfig(baseUrl, "valid_token", 0, 0, time.Minute), addInp) | ||
assert.Nil(t, actual, "Response for added tenant should have been nil") | ||
assert.NotNil(t, err, "Add tenant operation should have been an error") | ||
}) | ||
|
||
} |
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 tenants_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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
Paste your API token here | ||
add API token here |
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
resource "cdo_msp_managed_tenant" "tenant" { | ||
name = "test-tenant-name" | ||
display_name = "Display name for tenant" | ||
} | ||
|
||
resource "cdo_msp_managed_tenant" "existing_tenant" { | ||
api_token = "existing-tenant-api-token" | ||
} |
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
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,48 @@ | ||
package msp_tenant_test | ||
|
||
import ( | ||
"github.com/CiscoDevnet/terraform-provider-cdo/internal/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var testMspTenantResource = struct { | ||
Name string | ||
DisplayName string | ||
Id string | ||
Region string | ||
ApiToken string | ||
}{ | ||
ApiToken: acctest.Env.AddedMspManagedTenantApiToken(), | ||
Name: acctest.Env.AddedMspManagedTenantName(), | ||
DisplayName: acctest.Env.AddedMspManagedTenantDisplayName(), | ||
Id: acctest.Env.AddedMspManagedTenantId(), | ||
Region: strings.ToUpper(acctest.Env.MspTenantRegion()), | ||
} | ||
|
||
const testMspTenantResourceTemplate = ` | ||
resource "cdo_msp_managed_tenant" "test" { | ||
api_token = "{{.ApiToken}}" | ||
}` | ||
|
||
var testMspTenantResourceConfig = acctest.MustParseTemplate(testMspTenantResourceTemplate, testMspTenantResource) | ||
|
||
func TestAccMspTenantResource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: acctest.PreCheckFunc(t), | ||
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
Config: acctest.MspProviderConfig() + testMspTenantResourceConfig, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("cdo_msp_managed_tenant.test", "name", testMspTenantResource.Name), | ||
resource.TestCheckResourceAttr("cdo_msp_managed_tenant.test", "display_name", testMspTenantResource.DisplayName), | ||
resource.TestCheckResourceAttr("cdo_msp_managed_tenant.test", "id", testMspTenantResource.Id), | ||
resource.TestCheckResourceAttr("cdo_msp_managed_tenant.test", "region", testMspTenantResource.Region), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.