-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurerm_databox_edge_device
- swap to typed sdk, add data source. (#…
- Loading branch information
Showing
6 changed files
with
546 additions
and
200 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
165 changes: 165 additions & 0 deletions
165
internal/services/databoxedge/databox_edge_device_data_source.go
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,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) | ||
}, | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
internal/services/databoxedge/databox_edge_device_data_source_test.go
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,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)) | ||
} |
Oops, something went wrong.