Skip to content

Commit

Permalink
Add google_appengine_app resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ctavan committed Oct 5, 2017
1 parent ad69458 commit d863e79
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
9 changes: 9 additions & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/appengine/v1"
"google.golang.org/api/bigquery/v2"
"google.golang.org/api/cloudbilling/v1"
"google.golang.org/api/cloudresourcemanager/v1"
Expand All @@ -42,6 +43,7 @@ type Config struct {
Project string
Region string

clientAppengine *appengine.APIService
clientBilling *cloudbilling.Service
clientCompute *compute.Service
clientComputeBeta *computeBeta.Service
Expand Down Expand Up @@ -127,6 +129,13 @@ func (c *Config) loadAndValidate() error {

var err error

log.Printf("[INFO] Instantiating Google App Engine Client...")
c.clientAppengine, err = appengine.New(client)
if err != nil {
return err
}
c.clientAppengine.UserAgent = userAgent

log.Printf("[INFO] Instantiating GCE client...")
c.clientCompute, err = compute.New(client)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"google_appengine_app": resourceAppengineApp(),
"google_bigquery_dataset": resourceBigQueryDataset(),
"google_bigquery_table": resourceBigQueryTable(),
"google_bigtable_instance": resourceBigtableInstance(),
Expand Down
116 changes: 116 additions & 0 deletions google/resource_appengine_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package google

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"

"google.golang.org/api/appengine/v1"
"google.golang.org/api/googleapi"
)

func resourceAppengineApp() *schema.Resource {
return &schema.Resource{
Create: resourceAppengineAppCreate,
Read: resourceAppengineAppRead,
Delete: resourceAppengineAppDelete,

Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}

func resourceAppengineAppCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

region := d.Get("region").(string)

app := &appengine.Application{Id: project, LocationId: region}

var res *appengine.Operation

err = resource.Retry(1*time.Minute, func() *resource.RetryError {
call := config.clientAppengine.Apps.Create(app)

res, err = call.Do()
if err == nil {
return nil
}
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 429 {
return resource.RetryableError(gerr)
}
return resource.NonRetryableError(err)
})

if err != nil {
fmt.Printf("Error creating app engine app %s: %v", project, err)
return err
}

log.Printf("[DEBUG] Created App Engine app %v at location %v\n\n", project, region)

d.SetId(project)

return nil
}

func resourceAppengineAppRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

res, err := config.clientAppengine.Apps.Get(project).Do()

if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Removing App Engine app %q because it's gone", project)
// The resource doesn't exist anymore
d.SetId("")

return nil
}

return fmt.Errorf("Error reading App Engine app %s: %v", project, err)
}

log.Printf("[DEBUG] Read App Engine app %v at location %v\n\n", res.Id, res.LocationId)

d.SetId(res.Id)

return nil
}

func resourceAppengineAppDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

log.Printf("[DEBUG] Deleting App Engine app not supported %v\n\n", project)

return nil
}
38 changes: 38 additions & 0 deletions website/docs/r/appengine_app.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "google"
page_title: "Google: google_appengine_app"
sidebar_current: "docs-google-appengine-app"
description: |-
Creates an App Engine app
---

# google\_appengine\_app

Creates a Google App Engine app. For more information see
[the official documentation](https://cloud.google.com/appengine/docs/) and
[API](https://cloud.google.com/appengine/docs/admin-api/reference/rest/).


## Example Usage

```hcl
resource "google_appengine_app" "default" {
project = "my-google-project"
region = "us-central"
}
```

## Argument Reference

The following arguments are supported:

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

* `region` - (Optional) The region where the App Engine app will be deployed.
This is bound to the project and cannot be changed for the lifetime of the
project.

## Attributes Reference

Only the arguments listed above are exposed as attributes.

0 comments on commit d863e79

Please sign in to comment.