-
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.
Add 'azurerm_container_image' data source
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmContainerRegistry() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmContainerRegistryRead, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
MigrateState: resourceAzureRMContainerRegistryMigrateState, | ||
SchemaVersion: 2, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAzureRMContainerRegistryName, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
}, | ||
} | ||
} | ||
|
||
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) { | ||
log.Printf("[DEBUG] Container Registry %q was not found in Resource Group %q", name, resourceGroup) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
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 | ||
} |
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,46 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureRMContainerRegistry_basic(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccDataSourceAzureRMContainerRegistry_basic(ri) | ||
|
||
acrName := "azurerm_container_registry.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMContainerRegistryDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(acrName, "name"), | ||
resource.TestCheckResourceAttrSet(acrName, "resource_group_name"), | ||
resource.TestCheckResourceAttrSet(acrName, "location"), | ||
resource.TestCheckResourceAttrSet(acrName, "admin_enabled"), | ||
resource.TestCheckResourceAttrSet(acrName, "login_server"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMContainerRegistry_basic(rInt int) string { | ||
resource := testAccAzureRMContainerRegistry_basicManaged(rInt, testLocation(), "Basic") | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_container_registry" "test" { | ||
name = "testacccr%d" | ||
resource_group_name = "testAccRg-%d" | ||
} | ||
`, resource, rInt, rInt) | ||
} |
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