Skip to content

Commit

Permalink
azurerm_databox_edge_device - swap to typed sdk, add data source. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf authored Jan 9, 2023
1 parent ed7b09e commit 55ded18
Show file tree
Hide file tree
Showing 6 changed files with 546 additions and 200 deletions.
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

0 comments on commit 55ded18

Please sign in to comment.