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

Code generation from magic-module-specs#98 for Terraform #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
215 changes: 215 additions & 0 deletions azurerm/resource_arm_api_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file at
// https://github.com/Azure/magic-module-specs
//
// ----------------------------------------------------------------------------

package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiRelease() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiReleaseCreate,
Read: resourceArmApiReleaseRead,
Update: resourceArmApiReleaseUpdate,
Delete: resourceArmApiReleaseDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},

"resource_group": azure.SchemaResourceGroupNameDiffSuppress(),

"api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"release_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"api_id": {
Type: schema.TypeString,
Optional: true,
},

"notes": {
Type: schema.TypeString,
Optional: true,
},

// TODO: Property 'createdDateTime' of type Api::Azure::Type::ISO8601DateTime is not supported

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

// TODO: Property 'updatedDateTime' of type Api::Azure::Type::ISO8601DateTime is not supported
},
}
}

func resourceArmApiReleaseCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiReleaseClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group").(string)
apiID := d.Get("api_id").(string)
releaseID := d.Get("release_id").(string)
serviceName := d.Get("service_name").(string)

if requireResourcesToBeImported {
resp, err := client.Get(ctx, resourceGroup, serviceName, apiID, releaseID)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error checking for present of existing Api Release (Release %q / Api %q / Service Name %q / Resource Group %q): %+v", releaseID, apiID, serviceName, resourceGroup, err)
}
}
if !utils.ResponseWasNotFound(resp.Response) {
return tf.ImportAsExistsError("azurerm_api_release", *resp.ID)
}
}

apiId := d.Get("api_id").(string)
notes := d.Get("notes").(string)

parameters := apimanagement.ApiReleaseContract{
Properties: &apimanagement.ApiReleaseContractProperties{
ApiID: utils.String(apiId),
Notes: utils.String(notes),
},
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiID, releaseID, parameters); err != nil {
return fmt.Errorf("Error creating Api Release (Release %q / Api %q / Service Name %q / Resource Group %q): %+v", releaseID, apiID, serviceName, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, serviceName, apiID, releaseID)
if err != nil {
return fmt.Errorf("Error retrieving Api Release (Release %q / Api %q / Service Name %q / Resource Group %q): %+v", releaseID, apiID, serviceName, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read Api Release (Release %q / Api %q / Service Name %q / Resource Group %q) ID", releaseID, apiID, serviceName, resourceGroup)
}
d.SetId(*resp.ID)

return resourceArmApiReleaseRead(d, meta)
}

func resourceArmApiReleaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiReleaseClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
apiID := id.Path["apis"]
releaseID := id.Path["releases"]

resp, err := client.Get(ctx, resourceGroup, serviceName, apiID, releaseID)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Api Release %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Api Release (Release %q / Api %q / Service Name %q / Resource Group %q): %+v", releaseID, apiID, serviceName, resourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group", resourceGroup)
d.Set("api_id", apiID)
if properties := resp.Properties; properties != nil {
d.Set("api_id", properties.ApiID)
// TODO: SDK Reference /properties/createdDateTime is not supported
d.Set("notes", properties.Notes)
// TODO: SDK Reference /properties/updatedDateTime is not supported
}
d.Set("release_id", releaseID)
d.Set("service_name", serviceName)
d.Set("type", resp.Type)

return nil
}

func resourceArmApiReleaseUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiReleaseClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group").(string)
apiID := d.Get("api_id").(string)
apiId := d.Get("api_id").(string)
notes := d.Get("notes").(string)
releaseID := d.Get("release_id").(string)
serviceName := d.Get("service_name").(string)

parameters := apimanagement.ApiReleaseContract{
Properties: &apimanagement.ApiReleaseContractProperties{
ApiID: utils.String(apiId),
Notes: utils.String(notes),
},
}

if _, err := client.Update(ctx, resourceGroup, serviceName, apiID, releaseID, parameters); err != nil {
return fmt.Errorf("Error updating Api Release (Release %q / Api %q / Service Name %q / Resource Group %q): %+v", releaseID, apiID, serviceName, resourceGroup, err)
}

return resourceArmApiReleaseRead(d, meta)
}

func resourceArmApiReleaseDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiReleaseClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
apiID := id.Path["apis"]
releaseID := id.Path["releases"]

if _, err := client.Delete(ctx, resourceGroup, serviceName, apiID, releaseID); err != nil {
return fmt.Errorf("Error deleting Api Release (Release %q / Api %q / Service Name %q / Resource Group %q): %+v", releaseID, apiID, serviceName, resourceGroup, err)
}

return nil
}
75 changes: 75 additions & 0 deletions azurerm/resource_arm_api_release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file at
// https://github.com/Azure/magic-module-specs
//
// ----------------------------------------------------------------------------

package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func testCheckAzureRMApiReleaseExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Api Release not found: %s", resourceName)
}

resourceGroup := rs.Primary.Attributes["resource_group"]
apiID := rs.Primary.Attributes["api_id"]
releaseID := rs.Primary.Attributes["release_id"]
serviceName := rs.Primary.Attributes["service_name"]

client := testAccProvider.Meta().(*ArmClient).apiReleaseClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

if resp, err := client.Get(ctx, resourceGroup, serviceName, apiID, releaseID); err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Api Release (Release %q / Api %q / Service Name %q / Resource Group %q) does not exist", releaseID, apiID, serviceName, resourceGroup)
}
return fmt.Errorf("Bad: Get on apiReleaseClient: %+v", err)
}

return nil
}
}

func testCheckAzureRMApiReleaseDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).apiReleaseClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_api_release" {
continue
}

resourceGroup := rs.Primary.Attributes["resource_group"]
apiID := rs.Primary.Attributes["api_id"]
releaseID := rs.Primary.Attributes["release_id"]
serviceName := rs.Primary.Attributes["service_name"]

if resp, err := client.Get(ctx, resourceGroup, serviceName, apiID, releaseID); err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Get on apiReleaseClient: %+v", err)
}
}

return nil
}

return nil
}
55 changes: 55 additions & 0 deletions website/docs/r/api_release.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
#
# ----------------------------------------------------------------------------
#
# This file is automatically generated by Magic Modules and manual
# changes will be clobbered when the file is regenerated.
#
# Please read more about how to change this file at
# https://github.com/Azure/magic-module-specs
#
# ----------------------------------------------------------------------------
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_api_release"
sidebar_current: "docs-azurerm-resource-api-release"
description: |-
Manage Azure ApiRelease instance.
---

# azurerm_api_release

Manage Azure ApiRelease instance.


## Argument Reference

The following arguments are supported:

* `resource_group` - (Required) The name of the resource group. Changing this forces a new resource to be created.

* `api_id` - (Required) API identifier. Must be unique in the current API Management service instance. Changing this forces a new resource to be created.

* `release_id` - (Required) Release identifier within an API. Must be unique in the current API Management service instance. Changing this forces a new resource to be created.

* `service_name` - (Required) The name of the API Management service. Changing this forces a new resource to be created.

* `api_id` - (Optional) Identifier of the API the release belongs to.

* `notes` - (Optional) Release Notes

## Attributes Reference

The following attributes are exported:

* `created_date_time` - The time the API was released. The date conforms to the following format: yyyy-MM-ddTHH:mm:ssZ as specified by the ISO 8601 standard.

* `updated_date_time` - The time the API release was updated.

* `id` - Resource ID.

* `name` - Resource name.

* `type` - Resource type for API Management resource.