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

New resource/data source azurerm_storage_sync #7843

Merged
merged 6 commits into from
Sep 11, 2020
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
6 changes: 6 additions & 0 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
"github.com/Azure/azure-sdk-for-go/services/storagecache/mgmt/2019-11-01/storagecache"
"github.com/Azure/azure-sdk-for-go/services/storagesync/mgmt/2020-03-01/storagesync"
"github.com/Azure/go-autorest/autorest"
az "github.com/Azure/go-autorest/autorest/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
Expand All @@ -27,6 +28,7 @@ type Client struct {
BlobServicesClient storage.BlobServicesClient
CachesClient *storagecache.CachesClient
StorageTargetsClient *storagecache.StorageTargetsClient
SyncServiceClient *storagesync.ServicesClient
SubscriptionId string

environment az.Environment
Expand All @@ -52,6 +54,9 @@ func NewClient(options *common.ClientOptions) *Client {
storageTargetsClient := storagecache.NewStorageTargetsClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&storageTargetsClient.Client, options.ResourceManagerAuthorizer)

syncServiceClient := storagesync.NewServicesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&syncServiceClient.Client, options.ResourceManagerAuthorizer)

// TODO: switch Storage Containers to using the storage.BlobContainersClient
// (which should fix #2977) when the storage clients have been moved in here
client := Client{
Expand All @@ -62,6 +67,7 @@ func NewClient(options *common.ClientOptions) *Client {
CachesClient: &cachesClient,
SubscriptionId: options.SubscriptionId,
StorageTargetsClient: &storageTargetsClient,
SyncServiceClient: &syncServiceClient,
environment: options.Environment,
}

Expand Down
76 changes: 76 additions & 0 deletions azurerm/internal/services/storage/data_source_storage_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package storage

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmStorageSync() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmStorageSyncRead,

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.StorageSyncName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

"incoming_traffic_policy": {
Type: schema.TypeString,
Computed: true,
},

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceArmStorageSyncRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Storage.SyncServiceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Storage Sync %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("reading Storage Sync(Storage Sync Name %q / Resource Group %q): %+v", name, resGroup, err)
}

if id := resp.ID; id != nil {
d.SetId(*resp.ID)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
if props := resp.ServiceProperties; props != nil {
d.Set("incoming_traffic_policy", props.IncomingTrafficPolicy)
}
return tags.FlattenAndSet(d, resp.Tags)
}
26 changes: 26 additions & 0 deletions azurerm/internal/services/storage/parsers/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type AccountID struct {
ResourceGroup string
}

type StorageSyncId struct {
Name string
ResourceGroup string
}

func ParseAccountID(input string) (*AccountID, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
Expand All @@ -29,3 +34,24 @@ func ParseAccountID(input string) (*AccountID, error) {

return &account, nil
}

func ParseStorageSyncID(input string) (*StorageSyncId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

storageSync := StorageSyncId{
ResourceGroup: id.ResourceGroup,
}

if storageSync.Name, err = id.PopSegment("storageSyncServices"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &storageSync, nil
}
68 changes: 68 additions & 0 deletions azurerm/internal/services/storage/parsers/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,71 @@ func TestParseAccountID(t *testing.T) {
}
}
}

func TestParseStorageSyncID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *StorageSyncId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Storage Sync Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.StorageSync/storageSyncServices/",
Expected: nil,
},
{
Name: "Storage Sync ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.StorageSync/storageSyncServices/Sync1",
Expected: &StorageSyncId{
Name: "Sync1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.StorageSync/StorageSyncServices/Store1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ParseStorageSyncID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
2 changes: 2 additions & 0 deletions azurerm/internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_storage_container": dataSourceArmStorageContainer(),
"azurerm_storage_management_policy": dataSourceArmStorageManagementPolicy(),
"azurerm_storage_sync": dataSourceArmStorageSync(),
}
}

Expand All @@ -47,5 +48,6 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_storage_share_directory": resourceArmStorageShareDirectory(),
"azurerm_storage_table": resourceArmStorageTable(),
"azurerm_storage_table_entity": resourceArmStorageTableEntity(),
"azurerm_storage_sync": resourceArmStorageSync(),
}
}
Loading