Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #57 from h00130372/master
Browse files Browse the repository at this point in the history
Return service instance instead db instance for rds
  • Loading branch information
edisonxiang authored Sep 13, 2018
2 parents 1d6f248 + 994e7cb commit a0942ec
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 14 deletions.
14 changes: 14 additions & 0 deletions pkg/services/dcs/deprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,6 +39,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),
http.StatusUnprocessableEntity, "Can only delete dcs instance in ACTIVE or FAILED or CREATEFAILED")
}

// Init dcs client
dcsClient, err := b.CloudCredentials.DCSV1Client()
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/services/dcs/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -39,6 +40,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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/services/dms/instance/deprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,6 +39,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),
http.StatusUnprocessableEntity, "Can only delete dms instance in RUNNING or ERROR")
}

// Init dms client
dmsClient, err := b.CloudCredentials.DMSV1Client()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/services/dms/instance/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -39,6 +40,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),
http.StatusUnprocessableEntity, "Can only update dms instance in RUNNING")
}
// Init dms client
dmsClient, err := b.CloudCredentials.DMSV1Client()
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/services/rds/deprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,6 +39,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),
http.StatusUnprocessableEntity, "Can only delete rds instance in ACTIVE or FAILED")
}

// Init rds client
rdsClient, err := b.CloudCredentials.RDSV1Client()
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/services/rds/lastoperation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/services/rds/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -42,6 +43,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),
http.StatusUnprocessableEntity, "Can only update rds instance in ACTIVE")
}

// Init rds client
rdsClient, err := b.CloudCredentials.RDSV1Client()
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/services/rds/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand All @@ -59,27 +59,27 @@ 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.
Where("instance_id = ? and service_id = ? and plan_id = ?", instanceID, serviceID, planID).
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
Expand All @@ -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
}

0 comments on commit a0942ec

Please sign in to comment.