Skip to content

Commit

Permalink
Use pluginsdk shims, convert data.azuread_client_config to typed data…
Browse files Browse the repository at this point in the history
…source
  • Loading branch information
manicminer committed Sep 13, 2023
1 parent 2f4a871 commit 2eac106
Show file tree
Hide file tree
Showing 154 changed files with 3,332 additions and 3,254 deletions.
11 changes: 5 additions & 6 deletions internal/acceptance/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
)

type TestData struct {
Expand All @@ -28,7 +27,7 @@ type TestData struct {
// This is not securely generated and only suitable for ephemeral test cases
RandomPassword string

// ResourceName is the fully qualified resource name, comprising of the
// ResourceName is the fully qualified resource name, comprising the
// resource type and then the resource label
// e.g. `azuread_application.test`
ResourceName string
Expand All @@ -43,7 +42,7 @@ type TestData struct {
TenantID string
}

func (t *TestData) UUID() string {
func (t TestData) UUID() string {
uuid, err := uuid.GenerateUUID()
if err != nil {
panic(err)
Expand All @@ -56,7 +55,7 @@ func BuildTestData(t *testing.T, resourceType string, resourceLabel string) Test
EnsureProvidersAreInitialised()

testData := TestData{
RandomInteger: tf.AccRandTimeInt(),
RandomInteger: RandTimeInt(),
RandomString: acctest.RandString(5),
RandomPassword: fmt.Sprintf("%s%s", "p@$$Wd", acctest.RandString(6)),
ResourceName: fmt.Sprintf("%s.%s", resourceType, resourceLabel),
Expand All @@ -71,7 +70,7 @@ func BuildTestData(t *testing.T, resourceType string, resourceLabel string) Test
}

// RandomIntOfLength is a random 8 to 18 digit integer which is unique to this test case
func (td *TestData) RandomIntOfLength(len int) int {
func (td TestData) RandomIntOfLength(len int) int {
// len should not be
// - greater then 18, longest a int can represent
// - less then 8, as that gives us YYMMDDRR
Expand Down Expand Up @@ -99,7 +98,7 @@ func (td *TestData) RandomIntOfLength(len int) int {
}

// RandomStringOfLength is a random 1 to 1024 character string which is unique to this test case
func (td *TestData) RandomStringOfLength(len int) string {
func (td TestData) RandomStringOfLength(len int) string {
// len should not be less then 1 or greater than 1024
if 1 > len || len > 1024 {
panic("Invalid Test: RandomStringOfLength: length argument must be between 1 and 1024 characters")
Expand Down
13 changes: 11 additions & 2 deletions internal/tf/acctest.go → internal/acceptance/random.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tf
package acceptance

import (
"strconv"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
)

func AccRandTimeInt() int {
func RandTimeInt() int {
// acctest.RantInt() returns a value of size:
// 000000000000000000
// YYMMddHHmmsshhRRRR
Expand All @@ -28,3 +28,12 @@ func AccRandTimeInt() int {

return i
}

// RandString generates a random alphanumeric string of the length specified
func RandString(strlen int) string {
return acctest.RandString(strlen)
}

func RandStringFromCharSet(strlen int, charSet string) string {
return acctest.RandStringFromCharSet(strlen, charSet)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tf
package acceptance

import "testing"

func TestAccRandTimeInt(t *testing.T) {
t.Run("Rand Date int", func(t *testing.T) {
ri := AccRandTimeInt()
ri := RandTimeInt()

if ri < 100000000000000000 {
t.Fatalf("AccRandTimeInt returned a value (%d) shorter then expected", ri)
t.Fatalf("RandTimeInt returned a value (%d) shorter then expected", ri)
}

if ri > 999999999999999999 {
t.Fatalf("AccRandTimeInt returned a value (%d) longer then expected", ri)
t.Fatalf("RandTimeInt returned a value (%d) longer then expected", ri)
}
})
}
6 changes: 3 additions & 3 deletions internal/helpers/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk"
)

type ChangeFunc func(ctx context.Context) (*bool, error)
Expand All @@ -21,7 +21,7 @@ func WaitForDeletion(ctx context.Context, f ChangeFunc) error {
}

timeout := time.Until(deadline)
_, err := (&resource.StateChangeConf{ //nolint:staticcheck
_, err := (&pluginsdk.StateChangeConf{ //nolint:staticcheck
Pending: []string{"Waiting"},
Target: []string{"Deleted"},
Timeout: timeout,
Expand Down Expand Up @@ -56,7 +56,7 @@ func WaitForUpdate(ctx context.Context, f ChangeFunc) error {
}

func WaitForUpdateWithTimeout(ctx context.Context, timeout time.Duration, f ChangeFunc) (bool, error) {
res, err := (&resource.StateChangeConf{ //nolint:staticcheck
res, err := (&pluginsdk.StateChangeConf{ //nolint:staticcheck
Pending: []string{"Waiting"},
Target: []string{"Done"},
Timeout: timeout,
Expand Down
6 changes: 3 additions & 3 deletions internal/helpers/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azuread/internal/utils"
"github.com/manicminer/hamilton/msgraph"
)
Expand Down Expand Up @@ -87,7 +87,7 @@ func GetTokenSigningCertificateThumbprint(certByte []byte) (string, error) {
return buf.String(), nil
}

func KeyCredentialForResource(d *schema.ResourceData) (*msgraph.KeyCredential, error) {
func KeyCredentialForResource(d *pluginsdk.ResourceData) (*msgraph.KeyCredential, error) {
keyType := d.Get("type").(string)
value := d.Get("value").(string)

Expand Down Expand Up @@ -185,7 +185,7 @@ func KeyCredentialForResource(d *schema.ResourceData) (*msgraph.KeyCredential, e
return &credential, nil
}

func PasswordCredentialForResource(d *schema.ResourceData) (*msgraph.PasswordCredential, error) {
func PasswordCredentialForResource(d *pluginsdk.ResourceData) (*msgraph.PasswordCredential, error) {
credential := msgraph.PasswordCredential{}

// display_name, start_date and end_date support intentionally remains for if/when the API supports user-specified values for these
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"encoding/base64"
"fmt"
"github.com/hashicorp/terraform-provider-azuread/internal/sdk"
"log"
"os"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/sdk"
)

// Microsoft’s Terraform Partner ID is this specific GUID
Expand Down
16 changes: 10 additions & 6 deletions internal/sdk/plugin_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sdk

import (
"fmt"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk"

Check failure on line 8 in internal/sdk/plugin_sdk_test.go

View workflow job for this annotation

GitHub Actions / golint

File is not `goimports`-ed with -local github.com/hashicorp/terraform-provider-azuread/azuread (goimports)
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestAccPluginSDKAndDecoder(t *testing.T) {
},
},
},
Create: func(d *schema.ResourceData, i interface{}) error {
Create: func(d *schema.ResourceData, meta interface{}) error {
d.SetId("some-id")
d.Set("hello", "world")
d.Set("random_number", 42)
Expand Down Expand Up @@ -272,7 +273,7 @@ func TestAccPluginSDKAndDecoderOptionalComputed(t *testing.T) {
ResourcesMap: map[string]*schema.Resource{
"validator_decoder_specified": {
Schema: commonSchema,
Create: func(d *schema.ResourceData, i interface{}) error { //nolint:staticcheck
Create: func(d *schema.ResourceData, meta interface{}) error { //nolint:staticcheck
d.SetId("some-id")
return nil
},
Expand All @@ -288,7 +289,7 @@ func TestAccPluginSDKAndDecoderOptionalComputed(t *testing.T) {

"validator_decoder_unspecified": {
Schema: commonSchema,
Create: func(d *schema.ResourceData, i interface{}) error { //nolint:staticcheck
Create: func(d *schema.ResourceData, meta interface{}) error { //nolint:staticcheck
d.SetId("some-id")
d.Set("hello", "value-from-create")
d.Set("number", 42)
Expand Down Expand Up @@ -374,7 +375,7 @@ func TestAccPluginSDKAndDecoderOptionalComputedOverride(t *testing.T) {
Computed: true,
},
},
Create: func(d *schema.ResourceData, i interface{}) error { //nolint:staticcheck
Create: func(d *schema.ResourceData, meta interface{}) error { //nolint:staticcheck
d.SetId("some-id")
d.Set("hello", "value-from-create")
d.Set("number", 42)
Expand Down Expand Up @@ -493,7 +494,7 @@ func TestAccPluginSDKAndDecoderSets(t *testing.T) {
},
},
},
Create: func(d *schema.ResourceData, i interface{}) error { //nolint:staticcheck
Create: func(d *schema.ResourceData, meta interface{}) error { //nolint:staticcheck
d.SetId("some-id")
d.Set("set_of_strings", []string{
"some",
Expand Down Expand Up @@ -766,7 +767,7 @@ func TestAccPluginSDKAndEncoder(t *testing.T) {
},
},
},
Create: func(d *schema.ResourceData, i interface{}) error { //nolint:staticcheck
Create: func(d *schema.ResourceData, meta interface{}) error { //nolint:staticcheck
wrapper := ResourceMetaData{
ResourceData: d,
Logger: ConsoleLogger{},
Expand Down Expand Up @@ -991,6 +992,9 @@ func computedFieldsResource() *schema.Resource {
Delete: func(_ *schema.ResourceData, _ interface{}) error {
return nil
},
Importer: pluginsdk.ImporterValidatingResourceId(func(_ string) error {
return nil
}),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,71 @@ package administrativeunits
import (
"context"
"fmt"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/validation"

Check failure on line 9 in internal/services/administrativeunits/administrative_unit_data_source.go

View workflow job for this annotation

GitHub Actions / golint

File is not `goimports`-ed with -local github.com/hashicorp/terraform-provider-azuread/azuread (goimports)
"net/http"
"time"

"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/validate"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk"
"github.com/manicminer/hamilton/msgraph"
)

func administrativeUnitDataSource() *schema.Resource {
return &schema.Resource{
func administrativeUnitDataSource() *pluginsdk.Resource {
return &pluginsdk.Resource{
ReadContext: administrativeUnitDataSourceRead,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(5 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(5 * time.Minute),
Delete: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
Schema: map[string]*pluginsdk.Schema{
"object_id": {
Description: "The object ID of the administrative unit",
Type: schema.TypeString,
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateDiagFunc: validate.NoEmptyStrings,
ValidateDiagFunc: validation.ValidateDiag(validation.StringIsNotEmpty),
},

"display_name": {
Description: "The display name for the administrative unit",
Type: schema.TypeString,
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateDiagFunc: validate.NoEmptyStrings,
ValidateDiagFunc: validation.ValidateDiag(validation.StringIsNotEmpty),
},

"description": {
Description: "The description for the administrative unit",
Type: schema.TypeString,
Type: pluginsdk.TypeString,
Computed: true,
},

"members": {
Description: "A list of object IDs of members who are be present in this administrative unit.",
Type: schema.TypeList,
Type: pluginsdk.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"visibility": {
Description: "Whether the administrative unit and its members are hidden or publicly viewable in the directory",
Type: schema.TypeString,
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}

func administrativeUnitDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func administrativeUnitDataSourceRead(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*clients.Client).AdministrativeUnits.AdministrativeUnitsClient

var administrativeUnit msgraph.AdministrativeUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-azuread/internal/acceptance"
"github.com/hashicorp/terraform-provider-azuread/internal/acceptance/check"
)
Expand All @@ -17,10 +16,10 @@ type AdministrativeUnitDataSource struct{}
func TestAccAdministrativeUnitDataSource_byDisplayName(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_administrative_unit", "test")

data.DataSourceTest(t, []resource.TestStep{
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: AdministrativeUnitDataSource{}.displayName(data),
Check: resource.ComposeTestCheckFunc(
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("display_name").HasValue(fmt.Sprintf("acctestAdministrativeUnit-%d", data.RandomInteger)),
),
},
Expand All @@ -30,10 +29,10 @@ func TestAccAdministrativeUnitDataSource_byDisplayName(t *testing.T) {
func TestAccAdministrativeUnitDataSource_byObjectId(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_administrative_unit", "test")

data.DataSourceTest(t, []resource.TestStep{
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: AdministrativeUnitDataSource{}.objectId(data),
Check: resource.ComposeTestCheckFunc(
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("display_name").HasValue(fmt.Sprintf("acctestAdministrativeUnit-%d", data.RandomInteger)),
),
},
Expand All @@ -43,10 +42,10 @@ func TestAccAdministrativeUnitDataSource_byObjectId(t *testing.T) {
func TestAccAdministrativeUnitDataSource_members(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_administrative_unit", "test")

data.DataSourceTest(t, []resource.TestStep{
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: AdministrativeUnitDataSource{}.members(data),
Check: resource.ComposeTestCheckFunc(
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("display_name").HasValue(fmt.Sprintf("acctestAdministrativeUnit-%d", data.RandomInteger)),
check.That(data.ResourceName).Key("members.#").HasValue("4"),
),
Expand Down
Loading

0 comments on commit 2eac106

Please sign in to comment.