Skip to content

Commit

Permalink
Add a kubernetes style resource async class
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
chrisst authored and modular-magician committed Nov 26, 2019
1 parent 5e693fa commit fa07533
Show file tree
Hide file tree
Showing 25 changed files with 116 additions and 24 deletions.
116 changes: 116 additions & 0 deletions google/cloudrun_polling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package google

import (
"errors"
"fmt"

"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

const readyStatus string = "Ready"

type Condition struct {
Type string
Status string
Reason string
Message string
}

// KnativeStatus is a struct that can contain a Knative style resource's Status block. It is not
// intended to be used for anything other than polling for the success of the given resource.
type KnativeStatus struct {
Metadata struct {
Name string
Namespace string
SelfLink string
}
Status struct {
Conditions []Condition
}
}

// ConditionByType is a helper method for extracting a given condition
func (s KnativeStatus) ConditionByType(typ string) *Condition {
for _, condition := range s.Status.Conditions {
if condition.Type == typ {
c := Condition(condition)
return &c
}
}
return nil
}

// LatestMessage will return a human consumable status of the resource. This can
// be used to determine the human actionable error the GET doesn't return an explicit
// error but the resource is in an error state.
func (s KnativeStatus) LatestMessage() string {
c := s.ConditionByType(readyStatus)
if c != nil {
return fmt.Sprintf("%s - %s", c.Reason, c.Message)
}

return ""
}

// State will return a string representing the status of the Ready condition.
// No other conditions are currently returned as part of the state.
func (s KnativeStatus) State(res interface{}) string {
for _, condition := range s.Status.Conditions {
if condition.Type == "Ready" {
return fmt.Sprintf("%s:%s", condition.Type, condition.Status)
}
}
return "Empty"
}

// CloudRunPolling allows for polling against a cloud run resource that implements the
// Kubernetes style status schema.
type CloudRunPolling struct {
Config *Config
WaitURL string
}

func (p *CloudRunPolling) PendingStates() []string {
return []string{"Ready:Unknown", "Empty"}
}
func (p *CloudRunPolling) TargetStates() []string {
return []string{"Ready:True"}
}
func (p *CloudRunPolling) ErrorStates() []string {
return []string{"Ready:False"}
}

func cloudRunPollingWaitTime(config *Config, res map[string]interface{}, project, url, activity string, timeoutMinutes int) error {
w := &CloudRunPolling{}

scc := &resource.StateChangeConf{
Pending: w.PendingStates(),
Target: w.TargetStates(),
Refresh: func() (interface{}, string, error) {
res, err := sendRequest(config, "GET", project, url, nil)
if err != nil {
return res, "", err
}

status := KnativeStatus{}
err = Convert(res, &status)
if err != nil {
return res, "", err
}

for _, errState := range w.ErrorStates() {
if status.State(res) == errState {
err = errors.New(status.LatestMessage())
}
}

return res, status.State(res), err
},
Timeout: time.Duration(timeoutMinutes) * time.Minute,
}

_, err := scc.WaitForState()
return err
}
1 change: 0 additions & 1 deletion google/resource_access_context_manager_access_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ func resourceAccessContextManagerAccessLevelUpdate(d *schema.ResourceData, meta
err = accessContextManagerOperationWaitTime(
config, res, "Updating AccessLevel",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_access_context_manager_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ func resourceAccessContextManagerAccessPolicyUpdate(d *schema.ResourceData, meta
err = accessContextManagerOperationWaitTime(
config, res, "Updating AccessPolicy",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ func resourceAccessContextManagerServicePerimeterUpdate(d *schema.ResourceData,
err = accessContextManagerOperationWaitTime(
config, res, "Updating ServicePerimeter",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func resourceAppEngineApplicationUrlDispatchRulesUpdate(d *schema.ResourceData,
err = appEngineOperationWaitTime(
config, res, project, "Updating ApplicationUrlDispatchRules",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_app_engine_domain_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ func resourceAppEngineDomainMappingUpdate(d *schema.ResourceData, meta interface
err = appEngineOperationWaitTime(
config, res, project, "Updating DomainMapping",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_app_engine_standard_app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ func resourceAppEngineStandardAppVersionUpdate(d *schema.ResourceData, meta inte
err = appEngineOperationWaitTime(
config, res, project, "Updating StandardAppVersion",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) e
err = computeOperationWaitTime(
config, res, project, "Updating Autoscaler",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_backend_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func resourceComputeBackendBucketUpdate(d *schema.ResourceData, meta interface{}
err = computeOperationWaitTime(
config, res, project, "Updating BackendBucket",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,6 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
err = computeOperationWaitTime(
config, res, project, "Updating BackendService",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ func resourceComputeFirewallUpdate(d *schema.ResourceData, meta interface{}) err
err = computeOperationWaitTime(
config, res, project, "Updating Firewall",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,6 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
err = computeOperationWaitTime(
config, res, project, "Updating HealthCheck",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_http_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface
err = computeOperationWaitTime(
config, res, project, "Updating HttpHealthCheck",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_https_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ func resourceComputeHttpsHealthCheckUpdate(d *schema.ResourceData, meta interfac
err = computeOperationWaitTime(
config, res, project, "Updating HttpsHealthCheck",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_region_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ func resourceComputeRegionAutoscalerUpdate(d *schema.ResourceData, meta interfac
err = computeOperationWaitTime(
config, res, project, "Updating RegionAutoscaler",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
err = computeOperationWaitTime(
config, res, project, "Updating RegionBackendService",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ func resourceComputeRouterUpdate(d *schema.ResourceData, meta interface{}) error
err = computeOperationWaitTime(
config, res, project, "Updating Router",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ func resourceComputeRouterNatUpdate(d *schema.ResourceData, meta interface{}) er
err = computeOperationWaitTime(
config, res, project, "Updating RouterNat",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_ssl_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func resourceComputeSslPolicyUpdate(d *schema.ResourceData, meta interface{}) er
err = computeOperationWaitTime(
config, res, project, "Updating SslPolicy",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_compute_url_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ func resourceComputeUrlMapUpdate(d *schema.ResourceData, meta interface{}) error
err = computeOperationWaitTime(
config, res, project, "Updating UrlMap",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_filestore_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ func resourceFilestoreInstanceUpdate(d *schema.ResourceData, meta interface{}) e
err = filestoreOperationWaitTime(
config, res, project, "Updating Instance",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_redis_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ func resourceRedisInstanceUpdate(d *schema.ResourceData, meta interface{}) error
err = redisOperationWaitTime(
config, res, project, "Updating Instance",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_spanner_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ func resourceSpannerInstanceUpdate(d *schema.ResourceData, meta interface{}) err
err = spannerOperationWaitTime(
config, res, project, "Updating Instance",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ func resourceSQLDatabaseUpdate(d *schema.ResourceData, meta interface{}) error {
err = sqlAdminOperationWaitTime(
config, res, project, "Updating Database",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

Expand Down

0 comments on commit fa07533

Please sign in to comment.