mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
regions: lbagents: move pb update/create as mcclient module method
This commit is contained in:
@@ -27,7 +27,6 @@ import (
|
||||
"yunion.io/x/pkg/util/regutils"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
ansible_apis "yunion.io/x/onecloud/pkg/apis/ansible"
|
||||
compute_apis "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
identity_apis "yunion.io/x/onecloud/pkg/apis/identity"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
@@ -372,47 +371,10 @@ func (lbagent *SLoadbalancerAgent) updateOrCreatePbModel(ctx context.Context,
|
||||
pb *ansible.Playbook,
|
||||
) (*mcclient_models.AnsiblePlaybook, error) {
|
||||
cliSess := auth.GetSession(ctx, userCred, "", "")
|
||||
|
||||
if pbId == "" {
|
||||
pbJson, err := mcclient_modules.AnsiblePlaybooks.Get(cliSess, pbName, nil)
|
||||
if err == nil {
|
||||
pbModel := &mcclient_models.AnsiblePlaybook{}
|
||||
if err := pbJson.Unmarshal(pbModel); err == nil {
|
||||
pbId = pbModel.Id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pbJson jsonutils.JSONObject
|
||||
if pbId != "" {
|
||||
var err error
|
||||
ansiblePbInput := &ansible_apis.AnsiblePlaybookUpdateInput{
|
||||
Name: pbName,
|
||||
Playbook: *pb,
|
||||
}
|
||||
params := ansiblePbInput.JSON(ansiblePbInput)
|
||||
pbJson, err = mcclient_modules.AnsiblePlaybooks.Update(cliSess, pbId, params)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "update ansibleplaybook")
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
ansiblePbInput := &ansible_apis.AnsiblePlaybookCreateInput{
|
||||
Name: pbName,
|
||||
Playbook: *pb,
|
||||
}
|
||||
params := ansiblePbInput.JSON(ansiblePbInput)
|
||||
pbJson, err = mcclient_modules.AnsiblePlaybooks.Create(cliSess, params)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create ansibleplaybook")
|
||||
}
|
||||
}
|
||||
|
||||
pbModel := &mcclient_models.AnsiblePlaybook{}
|
||||
if err := pbJson.Unmarshal(pbModel); err != nil {
|
||||
return nil, errors.WithMessage(err, "unmarshal ansibleplaybook")
|
||||
}
|
||||
return pbModel, nil
|
||||
pbModel, err := mcclient_modules.AnsiblePlaybooks.UpdateOrCreatePbModel(
|
||||
ctx, cliSess, pbId, pbName, pb,
|
||||
)
|
||||
return pbModel, err
|
||||
}
|
||||
|
||||
func (lbagent *SLoadbalancerAgent) PerformUndeploy(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
|
||||
@@ -14,7 +14,18 @@
|
||||
|
||||
package modules
|
||||
|
||||
import "yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
ansible_apis "yunion.io/x/onecloud/pkg/apis/ansible"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
mcclient_models "yunion.io/x/onecloud/pkg/mcclient/models"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
||||
"yunion.io/x/onecloud/pkg/util/ansible"
|
||||
)
|
||||
|
||||
type AnsiblePlaybookManager struct {
|
||||
modulebase.ResourceManager
|
||||
@@ -41,3 +52,52 @@ func init() {
|
||||
}
|
||||
registerV2(&AnsiblePlaybooks)
|
||||
}
|
||||
|
||||
func (man *AnsiblePlaybookManager) UpdateOrCreatePbModel(
|
||||
ctx context.Context,
|
||||
cliSess *mcclient.ClientSession,
|
||||
pbId string,
|
||||
pbName string,
|
||||
pb *ansible.Playbook,
|
||||
) (*mcclient_models.AnsiblePlaybook, error) {
|
||||
if pbId == "" {
|
||||
pbJson, err := man.Get(cliSess, pbName, nil)
|
||||
if err == nil {
|
||||
pbModel := &mcclient_models.AnsiblePlaybook{}
|
||||
if err := pbJson.Unmarshal(pbModel); err == nil {
|
||||
pbId = pbModel.Id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pbJson jsonutils.JSONObject
|
||||
if pbId != "" {
|
||||
var err error
|
||||
ansiblePbInput := &ansible_apis.AnsiblePlaybookUpdateInput{
|
||||
Name: pbName,
|
||||
Playbook: *pb,
|
||||
}
|
||||
params := ansiblePbInput.JSON(ansiblePbInput)
|
||||
pbJson, err = man.Update(cliSess, pbId, params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "update ansibleplaybook")
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
ansiblePbInput := &ansible_apis.AnsiblePlaybookCreateInput{
|
||||
Name: pbName,
|
||||
Playbook: *pb,
|
||||
}
|
||||
params := ansiblePbInput.JSON(ansiblePbInput)
|
||||
pbJson, err = man.Create(cliSess, params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "create ansibleplaybook")
|
||||
}
|
||||
}
|
||||
|
||||
pbModel := &mcclient_models.AnsiblePlaybook{}
|
||||
if err := pbJson.Unmarshal(pbModel); err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshal ansibleplaybook")
|
||||
}
|
||||
return pbModel, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user