From 6994168a66cbedd5bd85a8b66839070765b978f0 Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 10:33:47 +0800 Subject: [PATCH 1/9] Return service instance instead db instance for rds --- pkg/services/rds/lastoperation.go | 14 +++++++------- pkg/services/rds/utils.go | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/services/rds/lastoperation.go b/pkg/services/rds/lastoperation.go index e5dfd731..b46c6ad0 100644 --- a/pkg/services/rds/lastoperation.go +++ b/pkg/services/rds/lastoperation.go @@ -14,7 +14,7 @@ func (b *RDSBroker) LastOperation(instanceID string, operationData database.Oper // Log opts b.Logger.Debug(fmt.Sprintf("lastoperation rds instance opts: instanceID: %s operationData: %v", instanceID, models.ToJson(operationData))) - dbInstance, err, serviceErr := SyncStatusWithService(b, instanceID, operationData.ServiceID, + instance, err, serviceErr := SyncStatusWithService(b, instanceID, operationData.ServiceID, operationData.PlanID, operationData.TargetID) if err != nil { @@ -32,20 +32,20 @@ func (b *RDSBroker) LastOperation(instanceID string, operationData database.Oper }, nil } // Status - if dbInstance.TargetStatus == "ACTIVE" { + if instance.Status == "ACTIVE" { return brokerapi.LastOperation{ State: brokerapi.Succeeded, - Description: fmt.Sprintf("Status: %s", dbInstance.TargetStatus), + Description: fmt.Sprintf("Status: %s", instance.Status), }, nil - } else if dbInstance.TargetStatus == "FAILED" { + } else if instance.Status == "FAILED" { return brokerapi.LastOperation{ State: brokerapi.Failed, - Description: fmt.Sprintf("Status: %s", dbInstance.TargetStatus), + Description: fmt.Sprintf("Status: %s", instance.Status), }, nil } else { return brokerapi.LastOperation{ State: brokerapi.InProgress, - Description: fmt.Sprintf("Status: %s", dbInstance.TargetStatus), + Description: fmt.Sprintf("Status: %s", instance.Status), }, nil } } else if operationData.OperationType == models.OperationDeprovisioning { @@ -66,7 +66,7 @@ func (b *RDSBroker) LastOperation(instanceID string, operationData database.Oper } else { return brokerapi.LastOperation{ State: brokerapi.InProgress, - Description: fmt.Sprintf("Status: %s", dbInstance.TargetStatus), + Description: fmt.Sprintf("Status: %s", instance.Status), }, nil } } else { diff --git a/pkg/services/rds/utils.go b/pkg/services/rds/utils.go index 059b0354..cea44fd9 100644 --- a/pkg/services/rds/utils.go +++ b/pkg/services/rds/utils.go @@ -50,7 +50,7 @@ func BuildBindingCredential( } func SyncStatusWithService(b *RDSBroker, instanceID string, serviceID string, planID string, - targetID string) (database.InstanceDetails, error, error) { + targetID string) (*instances.Instance, error, error) { dbInstance := database.InstanceDetails{} // Log opts b.Logger.Debug(fmt.Sprintf("SyncStatusWithService rds instance opts: instanceID: %s serviceID: %s " + @@ -59,12 +59,12 @@ func SyncStatusWithService(b *RDSBroker, instanceID string, serviceID string, pl // Init rds client rdsClient, err := b.CloudCredentials.RDSV1Client() if err != nil { - return dbInstance, fmt.Errorf("SyncStatusWithService create rds client failed. Error: %s", err), nil + return nil, fmt.Errorf("SyncStatusWithService create rds client failed. Error: %s", err), nil } // Invoke sdk get instance, serviceErr := instances.Get(rdsClient, targetID).Extract() if serviceErr != nil { - return dbInstance, nil, serviceErr + return nil, nil, serviceErr } // get InstanceDetails in back database err = database.BackDBConnection. @@ -72,14 +72,14 @@ func SyncStatusWithService(b *RDSBroker, instanceID string, serviceID string, pl First(&dbInstance).Error if err != nil { b.Logger.Debug(fmt.Sprintf("SyncStatusWithService get rds instance in back database failed. Error: %s", err)) - return dbInstance, fmt.Errorf("SyncStatusWithService get rds instance failed. Error: %s", err), nil + return instance, fmt.Errorf("SyncStatusWithService get rds instance failed. Error: %s", err), nil } // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("SyncStatusWithService rds instance in back database: %v", models.ToJson(dbInstance))) // update target info in back database targetInfo, err := json.Marshal(instance) if err != nil { - return dbInstance, fmt.Errorf("SyncStatusWithService marshal rds instance failed. Error: %s", err), nil + return instance, fmt.Errorf("SyncStatusWithService marshal rds instance failed. Error: %s", err), nil } dbInstance.TargetID = instance.ID @@ -91,9 +91,9 @@ func SyncStatusWithService(b *RDSBroker, instanceID string, serviceID string, pl if err != nil { b.Logger.Debug(fmt.Sprintf("SyncStatusWithService update rds instance target status in back database failed. " + "Error: %s", err)) - return dbInstance, fmt.Errorf("SyncStatusWithService update rds instance target status failed. Error: %s", err), nil + return instance, fmt.Errorf("SyncStatusWithService update rds instance target status failed. Error: %s", err), nil } // Sync target status success b.Logger.Debug(fmt.Sprintf("SyncStatusWithService update rds instance target status succeed: %s", instanceID)) - return dbInstance, nil, nil + return instance, nil, nil } From c060624f2f9fad98021eda72146237c2c2e73fb2 Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 11:04:11 +0800 Subject: [PATCH 2/9] Check status of rds instance when updating --- pkg/services/rds/update.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/services/rds/update.go b/pkg/services/rds/update.go index 88adb006..03070124 100644 --- a/pkg/services/rds/update.go +++ b/pkg/services/rds/update.go @@ -42,6 +42,19 @@ func (b *RDSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("rds instance in back database: %v", models.ToJson(ids))) + // sync and check status whether allowed to update + instance, err, serviceErr := SyncStatusWithService(b, instanceID, details.ServiceID, details.PlanID, ids.TargetID) + + if err != nil || serviceErr != nil { + return brokerapi.UpdateServiceSpec{}, fmt.Errorf("sync status failed. error: %s, service error: %s", err, serviceErr) + } + if instance.Status != "ACTIVE" { + return brokerapi.UpdateServiceSpec{}, + brokerapi.NewFailureResponse( + fmt.Errorf("Can only update rds instance in ACTIVE, but in: %s", instance.Status), + 422, "Can only update rds instance in ACTIVE") + } + // Init rds client rdsClient, err := b.CloudCredentials.RDSV1Client() if err != nil { From a754c06e53a7649c3d6161b7da81fe7942309714 Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 15:21:22 +0800 Subject: [PATCH 3/9] Check status of rds instance when deprovisioning --- pkg/services/rds/deprovision.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/services/rds/deprovision.go b/pkg/services/rds/deprovision.go index e32cb7e8..26a3d88b 100644 --- a/pkg/services/rds/deprovision.go +++ b/pkg/services/rds/deprovision.go @@ -38,6 +38,19 @@ func (b *RDSBroker) Deprovision(instanceID string, details brokerapi.Deprovision // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("rds instance in back database: %v", models.ToJson(ids))) + // sync and check status whether allowed to update + instance, err, serviceErr := SyncStatusWithService(b, instanceID, details.ServiceID, details.PlanID, ids.TargetID) + + if err != nil || serviceErr != nil { + return brokerapi.DeprovisionServiceSpec{}, fmt.Errorf("sync status failed. error: %s, service error: %s", err, serviceErr) + } + if instance.Status != "ACTIVE" && instance.Status != "FAILED" { + return brokerapi.DeprovisionServiceSpec{}, + brokerapi.NewFailureResponse( + fmt.Errorf("Can only delete rds instance in ACTIVE or FAILED, but in: %s", instance.Status), + 422, "Can only delete rds instance in ACTIVE or FAILED") + } + // Init rds client rdsClient, err := b.CloudCredentials.RDSV1Client() if err != nil { From 8bb69bc2b5bcafe27b145b21f478320b7224d586 Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 16:20:42 +0800 Subject: [PATCH 4/9] Check status for dcs instance when updating --- pkg/services/dcs/update.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/services/dcs/update.go b/pkg/services/dcs/update.go index f222e687..e44c48ca 100644 --- a/pkg/services/dcs/update.go +++ b/pkg/services/dcs/update.go @@ -39,6 +39,19 @@ func (b *DCSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("dcs instance in back database: %v", models.ToJson(ids))) + // sync and check status whether allowed to update + instance, err, serviceErr := SyncStatusWithService(b, instanceID, details.ServiceID, details.PlanID, ids.TargetID) + + if err != nil || serviceErr != nil { + return brokerapi.UpdateServiceSpec{}, fmt.Errorf("sync status failed. error: %s, service error: %s", err, serviceErr) + } + if instance.Status != "RUNNING" { + return brokerapi.UpdateServiceSpec{}, + brokerapi.NewFailureResponse( + fmt.Errorf("Can only update dcs instance in RUNNING, but in: %s", instance.Status), + http.StatusUnprocessableEntity, "Can only update dcs instance in RUNNING") + } + // Init dcs client dcsClient, err := b.CloudCredentials.DCSV1Client() if err != nil { From ab18283e8cfb37b13ba40b6c94fa6abb289629c2 Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 16:28:51 +0800 Subject: [PATCH 5/9] Check status for dcs instance when deprovisioning --- pkg/services/dcs/deprovision.go | 13 +++++++++++++ pkg/services/dcs/update.go | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/services/dcs/deprovision.go b/pkg/services/dcs/deprovision.go index 472e8b8a..490d1362 100644 --- a/pkg/services/dcs/deprovision.go +++ b/pkg/services/dcs/deprovision.go @@ -38,6 +38,19 @@ func (b *DCSBroker) Deprovision(instanceID string, details brokerapi.Deprovision // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("dcs instance in back database: %v", models.ToJson(ids))) + // sync and check status whether allowed to update + instance, err, serviceErr := SyncStatusWithService(b, instanceID, details.ServiceID, details.PlanID, ids.TargetID) + + if err != nil || serviceErr != nil { + return brokerapi.DeprovisionServiceSpec{}, fmt.Errorf("sync status failed. error: %s, service error: %s", err, serviceErr) + } + if instance.Status != "RUNNING" && instance.Status != "ERROR" && instance.Status != "CREATEFAILED" { + return brokerapi.DeprovisionServiceSpec{}, + brokerapi.NewFailureResponse( + fmt.Errorf("Can only delete dcs instance in ACTIVE or FAILED or CREATEFAILED, but in: %s", instance.Status), + 422, "Can only delete dcs instance in ACTIVE or FAILED or CREATEFAILED") + } + // Init dcs client dcsClient, err := b.CloudCredentials.DCSV1Client() if err != nil { diff --git a/pkg/services/dcs/update.go b/pkg/services/dcs/update.go index e44c48ca..59dc5bfd 100644 --- a/pkg/services/dcs/update.go +++ b/pkg/services/dcs/update.go @@ -49,7 +49,7 @@ func (b *DCSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a return brokerapi.UpdateServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only update dcs instance in RUNNING, but in: %s", instance.Status), - http.StatusUnprocessableEntity, "Can only update dcs instance in RUNNING") + 422, "Can only update dcs instance in RUNNING") } // Init dcs client From 1dd09a413dd3002ab36ebede6f5b628e9049d6aa Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 16:31:42 +0800 Subject: [PATCH 6/9] Check status for dms instance when updating --- pkg/services/dms/instance/update.go | 12 ++++++++++++ "\357\274\201" | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 "\357\274\201" diff --git a/pkg/services/dms/instance/update.go b/pkg/services/dms/instance/update.go index a7268505..4886f8a5 100644 --- a/pkg/services/dms/instance/update.go +++ b/pkg/services/dms/instance/update.go @@ -39,6 +39,18 @@ func (b *DMSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("dms instance in back database: %v", models.ToJson(ids))) + // sync and check status whether allowed to update + instance, err, serviceErr := SyncStatusWithService(b, instanceID, details.ServiceID, details.PlanID, ids.TargetID) + + if err != nil || serviceErr != nil { + return brokerapi.UpdateServiceSpec{}, fmt.Errorf("sync status failed. error: %s, service error: %s", err, serviceErr) + } + if instance.Status != "RUNNING" { + return brokerapi.UpdateServiceSpec{}, + brokerapi.NewFailureResponse( + fmt.Errorf("Can only update dms instance in RUNNING, but in: %s", instance.Status), + 422, "Can only update dms instance in RUNNING") + } // Init dms client dmsClient, err := b.CloudCredentials.DMSV1Client() if err != nil { diff --git "a/\357\274\201" "b/\357\274\201" new file mode 100644 index 00000000..8dba8d3e --- /dev/null +++ "b/\357\274\201" @@ -0,0 +1,11 @@ +Check status for dcs instance when deprovisioning +# Please enter the commit message for your changes. Lines starting +# with '#' will be ignored, and an empty message aborts the commit. +# +# On branch master +# Your branch is up to date with 'origin/master'. +# +# Changes to be committed: +# modified: pkg/services/dcs/deprovision.go +# modified: pkg/services/dcs/update.go +# From 9c7fa3f535789d9c892a883e441166a54eb567fa Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 16:37:17 +0800 Subject: [PATCH 7/9] Check status for dms instance when deprovisioning --- pkg/services/dms/instance/deprovision.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/services/dms/instance/deprovision.go b/pkg/services/dms/instance/deprovision.go index cb872796..97dc3abd 100644 --- a/pkg/services/dms/instance/deprovision.go +++ b/pkg/services/dms/instance/deprovision.go @@ -38,6 +38,19 @@ func (b *DMSBroker) Deprovision(instanceID string, details brokerapi.Deprovision // Log InstanceDetails b.Logger.Debug(fmt.Sprintf("dms instance in back database: %v", models.ToJson(ids))) + // sync and check status whether allowed to update + instance, err, serviceErr := SyncStatusWithService(b, instanceID, details.ServiceID, details.PlanID, ids.TargetID) + + if err != nil || serviceErr != nil { + return brokerapi.DeprovisionServiceSpec{}, fmt.Errorf("sync status failed. error: %s, service error: %s", err, serviceErr) + } + if instance.Status != "RUNNING" && instance.Status != "ERROR" && instance.Status != "CREATEFAILED" { + return brokerapi.DeprovisionServiceSpec{}, + brokerapi.NewFailureResponse( + fmt.Errorf("Can only delete dms instance in RUNNING or ERROR, but in: %s", instance.Status), + 422, "Can only delete dms instance in RUNNING or ERROR") + } + // Init dms client dmsClient, err := b.CloudCredentials.DMSV1Client() if err != nil { From 5bb0cbdfdcb195600c7899688f4afc31ff54a0b5 Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 17:11:33 +0800 Subject: [PATCH 8/9] Use http.StatusUnprocessableEntity instead --- pkg/services/dcs/deprovision.go | 3 ++- pkg/services/dcs/update.go | 3 ++- pkg/services/dms/instance/deprovision.go | 3 ++- pkg/services/dms/instance/update.go | 3 ++- pkg/services/rds/deprovision.go | 5 +++-- pkg/services/rds/update.go | 3 ++- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/services/dcs/deprovision.go b/pkg/services/dcs/deprovision.go index 490d1362..80d99b4e 100644 --- a/pkg/services/dcs/deprovision.go +++ b/pkg/services/dcs/deprovision.go @@ -2,6 +2,7 @@ package dcs import ( "fmt" + "net/http" "github.com/huaweicloud/golangsdk/openstack/dcs/v1/instances" "github.com/huaweicloud/huaweicloud-service-broker/pkg/database" @@ -48,7 +49,7 @@ func (b *DCSBroker) Deprovision(instanceID string, details brokerapi.Deprovision return brokerapi.DeprovisionServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only delete dcs instance in ACTIVE or FAILED or CREATEFAILED, but in: %s", instance.Status), - 422, "Can only delete dcs instance in ACTIVE or FAILED or CREATEFAILED") + http.StatusUnprocessableEntity, "Can only delete dcs instance in ACTIVE or FAILED or CREATEFAILED") } // Init dcs client diff --git a/pkg/services/dcs/update.go b/pkg/services/dcs/update.go index 59dc5bfd..55b9bbee 100644 --- a/pkg/services/dcs/update.go +++ b/pkg/services/dcs/update.go @@ -3,6 +3,7 @@ package dcs import ( "encoding/json" "fmt" + "net/http" "github.com/huaweicloud/golangsdk/openstack/dcs/v1/instances" "github.com/huaweicloud/huaweicloud-service-broker/pkg/database" @@ -49,7 +50,7 @@ func (b *DCSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a return brokerapi.UpdateServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only update dcs instance in RUNNING, but in: %s", instance.Status), - 422, "Can only update dcs instance in RUNNING") + http.StatusUnprocessableEntity, "Can only update dcs instance in RUNNING") } // Init dcs client diff --git a/pkg/services/dms/instance/deprovision.go b/pkg/services/dms/instance/deprovision.go index 97dc3abd..60219f1b 100644 --- a/pkg/services/dms/instance/deprovision.go +++ b/pkg/services/dms/instance/deprovision.go @@ -2,6 +2,7 @@ package instance import ( "fmt" + "net/http" "github.com/huaweicloud/golangsdk/openstack/dms/v1/instances" "github.com/huaweicloud/huaweicloud-service-broker/pkg/database" @@ -48,7 +49,7 @@ func (b *DMSBroker) Deprovision(instanceID string, details brokerapi.Deprovision return brokerapi.DeprovisionServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only delete dms instance in RUNNING or ERROR, but in: %s", instance.Status), - 422, "Can only delete dms instance in RUNNING or ERROR") + http.StatusUnprocessableEntity, "Can only delete dms instance in RUNNING or ERROR") } // Init dms client diff --git a/pkg/services/dms/instance/update.go b/pkg/services/dms/instance/update.go index 4886f8a5..7dad48da 100644 --- a/pkg/services/dms/instance/update.go +++ b/pkg/services/dms/instance/update.go @@ -3,6 +3,7 @@ package instance import ( "encoding/json" "fmt" + "net/http" "github.com/huaweicloud/golangsdk/openstack/dms/v1/instances" "github.com/huaweicloud/huaweicloud-service-broker/pkg/database" @@ -49,7 +50,7 @@ func (b *DMSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a return brokerapi.UpdateServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only update dms instance in RUNNING, but in: %s", instance.Status), - 422, "Can only update dms instance in RUNNING") + http.StatusUnprocessableEntity, "Can only update dms instance in RUNNING") } // Init dms client dmsClient, err := b.CloudCredentials.DMSV1Client() diff --git a/pkg/services/rds/deprovision.go b/pkg/services/rds/deprovision.go index 26a3d88b..13284f23 100644 --- a/pkg/services/rds/deprovision.go +++ b/pkg/services/rds/deprovision.go @@ -2,6 +2,7 @@ package rds import ( "fmt" + "net/http" "github.com/huaweicloud/golangsdk/openstack/rds/v1/instances" "github.com/huaweicloud/huaweicloud-service-broker/pkg/database" @@ -48,9 +49,9 @@ func (b *RDSBroker) Deprovision(instanceID string, details brokerapi.Deprovision return brokerapi.DeprovisionServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only delete rds instance in ACTIVE or FAILED, but in: %s", instance.Status), - 422, "Can only delete rds instance in ACTIVE or FAILED") + http.StatusUnprocessableEntity, "Can only delete rds instance in ACTIVE or FAILED") } - + // Init rds client rdsClient, err := b.CloudCredentials.RDSV1Client() if err != nil { diff --git a/pkg/services/rds/update.go b/pkg/services/rds/update.go index 03070124..4e17b9ed 100644 --- a/pkg/services/rds/update.go +++ b/pkg/services/rds/update.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "net/http" "github.com/huaweicloud/golangsdk/openstack/rds/v1/datastores" "github.com/huaweicloud/golangsdk/openstack/rds/v1/flavors" @@ -52,7 +53,7 @@ func (b *RDSBroker) Update(instanceID string, details brokerapi.UpdateDetails, a return brokerapi.UpdateServiceSpec{}, brokerapi.NewFailureResponse( fmt.Errorf("Can only update rds instance in ACTIVE, but in: %s", instance.Status), - 422, "Can only update rds instance in ACTIVE") + http.StatusUnprocessableEntity, "Can only update rds instance in ACTIVE") } // Init rds client From 994e7cbb0bc03f3e63c858a91f8aa7901cc2d31a Mon Sep 17 00:00:00 2001 From: h00130372 Date: Wed, 12 Sep 2018 17:39:48 +0800 Subject: [PATCH 9/9] Fix --- "\357\274\201" | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 "\357\274\201" diff --git "a/\357\274\201" "b/\357\274\201" deleted file mode 100644 index 8dba8d3e..00000000 --- "a/\357\274\201" +++ /dev/null @@ -1,11 +0,0 @@ -Check status for dcs instance when deprovisioning -# Please enter the commit message for your changes. Lines starting -# with '#' will be ignored, and an empty message aborts the commit. -# -# On branch master -# Your branch is up to date with 'origin/master'. -# -# Changes to be committed: -# modified: pkg/services/dcs/deprovision.go -# modified: pkg/services/dcs/update.go -#