Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource/Data source
azurerm_data_share_account
(#6575)
fix #6480
- Loading branch information
Showing
34 changed files
with
15,296 additions
and
1 deletion.
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
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
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,19 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/services/datashare/mgmt/2019-11-01/datashare" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common" | ||
) | ||
|
||
type Client struct { | ||
AccountClient *datashare.AccountsClient | ||
} | ||
|
||
func NewClient(o *common.ClientOptions) *Client { | ||
accountClient := datashare.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) | ||
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer) | ||
|
||
return &Client{ | ||
AccountClient: &accountClient, | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
azurerm/internal/services/datashare/data_source_data_share_account.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,89 @@ | ||
package datashare | ||
|
||
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/services/datashare/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 dataSourceDataShareAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmDataShareAccountRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.DataShareAccountName(), | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"identity": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"principal_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tenant_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"tags": tags.SchemaDataSource(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmDataShareAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).DataShare.AccountClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] DataShare %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("retrieving DataShare Account %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("reading DataShare Account %q (Resource Group %q): ID is empty or nil", name, resourceGroup) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
if err := d.Set("identity", flattenAzureRmDataShareAccountIdentity(resp.Identity)); err != nil { | ||
return fmt.Errorf("setting `identity`: %+v", err) | ||
} | ||
return tags.FlattenAndSet(d, resp.Tags) | ||
} |
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,31 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type DataShareAccountId struct { | ||
ResourceGroup string | ||
Name string | ||
} | ||
|
||
func DataShareAccountID(input string) (*DataShareAccountId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing DataShareAccount ID %q: %+v", input, err) | ||
} | ||
|
||
dataShareAccount := DataShareAccountId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
if dataShareAccount.Name, err = id.PopSegment("accounts"); err != nil { | ||
return nil, err | ||
} | ||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &dataShareAccount, nil | ||
} |
72 changes: 72 additions & 0 deletions
72
azurerm/internal/services/datashare/parse/data_share_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,72 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDataShareAccountID(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *DataShareAccountId | ||
}{ | ||
{ | ||
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 Account Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Datashare account ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1", | ||
Expected: &DataShareAccountId{ | ||
Name: "account1", | ||
ResourceGroup: "resGroup1", | ||
}, | ||
}, | ||
{ | ||
Name: "Wrong Casing", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/Accounts/account1", | ||
Expected: nil, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q..", v.Name) | ||
|
||
actual, err := DataShareAccountID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
} | ||
} |
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,31 @@ | ||
package datashare | ||
|
||
import "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
|
||
type Registration struct{} | ||
|
||
// Name is the name of this Service | ||
func (r Registration) Name() string { | ||
return "Data Share" | ||
} | ||
|
||
// WebsiteCategories returns a list of categories which can be used for the sidebar | ||
func (r Registration) WebsiteCategories() []string { | ||
return []string{ | ||
"Data Share", | ||
} | ||
} | ||
|
||
// SupportedDataSources returns the supported Data Sources supported by this Service | ||
func (r Registration) SupportedDataSources() map[string]*schema.Resource { | ||
return map[string]*schema.Resource{ | ||
"azurerm_data_share_account": dataSourceDataShareAccount(), | ||
} | ||
} | ||
|
||
// SupportedResources returns the supported Resources supported by this Service | ||
func (r Registration) SupportedResources() map[string]*schema.Resource { | ||
return map[string]*schema.Resource{ | ||
"azurerm_data_share_account": resourceArmDataShareAccount(), | ||
} | ||
} |
Oops, something went wrong.