Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_databox_edge_device - swap to typed sdk, add data source. #19914

Merged
merged 3 commits into from
Jan 9, 2023
Merged
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
1 change: 1 addition & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
cosmos.Registration{},
costmanagement.Registration{},
dashboard.Registration{},
databoxedge.Registration{},
databricks.Registration{},
digitaltwins.Registration{},
disks.Registration{},
Expand Down
165 changes: 165 additions & 0 deletions internal/services/databoxedge/databox_edge_device_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package databoxedge

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/databoxedge/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/databoxedge/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type EdgeDeviceDataSource struct{}

var _ sdk.DataSource = EdgeDeviceDataSource{}

func (d EdgeDeviceDataSource) Arguments() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataboxEdgeName,
},

"resource_group_name": commonschema.ResourceGroupName(),
}
}

func (d EdgeDeviceDataSource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{

"location": commonschema.LocationComputed(),

"sku_name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"device_properties": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"configured_role_types": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"culture": {
Type: pluginsdk.TypeString,
Computed: true,
},

"hcs_version": {
Type: pluginsdk.TypeString,
Computed: true,
},

"capacity": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"model": {
Type: pluginsdk.TypeString,
Computed: true,
},

"status": {
Type: pluginsdk.TypeString,
Computed: true,
},

"software_version": {
Type: pluginsdk.TypeString,
Computed: true,
},

"type": {
Type: pluginsdk.TypeString,
Computed: true,
},

"node_count": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"serial_number": {
Type: pluginsdk.TypeString,
Computed: true,
},

"time_zone": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"tags": commonschema.TagsDataSource(),
}
}

func (d EdgeDeviceDataSource) ModelObject() interface{} {
return &EdgeDeviceModel{}
}

func (d EdgeDeviceDataSource) ResourceType() string {
return "azurerm_databox_edge_device"
}

func (d EdgeDeviceDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
subscriptionId := metadata.Client.Account.SubscriptionId
client := metadata.Client.DataboxEdge.DeviceClient

var metaModel EdgeDeviceModel
if err := metadata.Decode(&metaModel); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

id := parse.NewDeviceID(subscriptionId, metaModel.ResourceGroupName, metaModel.Name)

resp, err := client.Get(ctx, id.DataBoxEdgeDeviceName, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] %s was not found - removing from state", id)
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

state := EdgeDeviceModel{
Name: id.DataBoxEdgeDeviceName,
ResourceGroupName: id.ResourceGroup,
Location: location.NormalizeNilable(resp.Location),
}

if props := resp.DeviceProperties; props != nil {
state.DeviceProperties = flattenDeviceProperties(props)
state.SkuName = flattenDeviceSku(resp.Sku)
state.Tags = tags.ToTypedObject(resp.Tags)
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package databoxedge_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type DataboxEdgeDeviceDataSource struct{}

func TestAccDataboxEdgeDeviceDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_databox_edge_device", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: DataboxEdgeDeviceDataSource{}.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").Exists(),
check.That(data.ResourceName).Key("sku_name").HasValue("EdgeP_Base-Standard"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
),
},
data.ImportStep(),
},
)
}

func (r DataboxEdgeDeviceDataSource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_databox_edge_device" "test" {
name = azurerm_databox_edge_device.test.name
resource_group_name = azurerm_databox_edge_device.test.resource_group_name
}

`, DataboxEdgeDeviceResource{}.complete(data))
}
Loading