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

Add App Engine Application support #1503

Merged
merged 5 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
83 changes: 83 additions & 0 deletions google/appengine_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package google

import (
"fmt"
"log"
"regexp"
"strconv"
"time"

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

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

var (
appEngineOperationIdRE = regexp.MustCompile(fmt.Sprintf("apps/%s/operations/(.*)", ProjectRegex))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry this is such a nit to pick, but IdRE bugs me - can it be IdRegex or IdRegEx or IDReg or IdReg or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be! I'll push an update for you to approve momentarily.

)

type AppEngineOperationWaiter struct {
Service *appengine.APIService
Op *appengine.Operation
AppId string
}

func (w *AppEngineOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
matches := appEngineOperationIdRE.FindStringSubmatch(w.Op.Name)
if len(matches) != 2 {
return nil, "", fmt.Errorf("Expected %d results of parsing operation name, got %d from %s", 2, len(matches), w.Op.Name)
}
op, err := w.Service.Apps.Operations.Get(w.AppId, matches[1]).Do()
if err != nil {
return nil, "", err
}

log.Printf("[DEBUG] Got %v when asking for operation %q", op.Done, w.Op.Name)
return op, strconv.FormatBool(op.Done), nil
}
}

func (w *AppEngineOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"false"},
Target: []string{"true"},
Refresh: w.RefreshFunc(),
}
}

// AppEngineOperationError wraps appengine.Status and implements the
// error interface so it can be returned.
type AppEngineOperationError appengine.Status

func (e AppEngineOperationError) Error() string {
return e.Message
}

func appEngineOperationWait(client *appengine.APIService, op *appengine.Operation, appId, activity string) error {
return appEngineOperationWaitTime(client, op, appId, activity, 4)
}

func appEngineOperationWaitTime(client *appengine.APIService, op *appengine.Operation, appId, activity string, timeoutMin int) error {
w := &AppEngineOperationWaiter{
Service: client,
Op: op,
AppId: appId,
}

state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = time.Duration(timeoutMin) * time.Minute
state.MinTimeout = 2 * time.Second
opRaw, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}

resultOp := opRaw.(*appengine.Operation)
if resultOp.Error != nil {
return AppEngineOperationError(*resultOp.Error)
}

return nil
}
9 changes: 9 additions & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
appengine "google.golang.org/api/appengine/v1"
"google.golang.org/api/bigquery/v2"
"google.golang.org/api/cloudbilling/v1"
"google.golang.org/api/cloudfunctions/v1"
Expand Down Expand Up @@ -76,6 +77,7 @@ type Config struct {
clientBigQuery *bigquery.Service
clientCloudFunctions *cloudfunctions.Service
clientCloudIoT *cloudiot.Service
clientAppEngine *appengine.APIService

bigtableClientFactory *BigtableClientFactory
}
Expand Down Expand Up @@ -315,6 +317,13 @@ func (c *Config) loadAndValidate() error {
}
c.clientCloudIoT.UserAgent = userAgent

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

return nil
}

Expand Down
Loading