forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_arm_container_registry.go
104 lines (88 loc) · 2.62 KB
/
data_source_arm_container_registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package azurerm
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmContainerRegistry() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmContainerRegistryRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMContainerRegistryName,
},
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"location": locationForDataSourceSchema(),
"admin_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"admin_password": {
Type: schema.TypeString,
Computed: true,
},
"admin_username": {
Type: schema.TypeString,
Computed: true,
},
"login_server": {
Type: schema.TypeString,
Computed: true,
},
"sku": {
Type: schema.TypeString,
Computed: true,
},
"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).containerRegistryClient
ctx := meta.(*ArmClient).StopContext
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) {
return fmt.Errorf("Container Registry %q was not found in Resource Group %q", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on Azure Container Registry %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(*resp.ID)
d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
d.Set("admin_enabled", resp.AdminUserEnabled)
d.Set("login_server", resp.LoginServer)
if sku := resp.Sku; sku != nil {
d.Set("sku", string(sku.Tier))
}
if account := resp.StorageAccount; account != nil {
d.Set("storage_account_id", account.ID)
}
if *resp.AdminUserEnabled {
credsResp, err := client.ListCredentials(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Container Registry %s for Credentials: %s", name, err)
}
d.Set("admin_username", credsResp.Username)
for _, v := range *credsResp.Passwords {
d.Set("admin_password", v.Value)
break
}
} else {
d.Set("admin_username", "")
d.Set("admin_password", "")
}
flattenAndSetTags(d, resp.Tags)
return nil
}