mod_ansibleplaybooks.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ansible
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. ansible_apis "yunion.io/x/onecloud/pkg/apis/ansible"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. mcclient_models "yunion.io/x/onecloud/pkg/mcclient/models"
  22. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules"
  24. "yunion.io/x/onecloud/pkg/util/ansible"
  25. )
  26. type AnsiblePlaybookManager struct {
  27. modulebase.ResourceManager
  28. }
  29. var (
  30. AnsiblePlaybooks AnsiblePlaybookManager
  31. )
  32. func init() {
  33. AnsiblePlaybooks = AnsiblePlaybookManager{
  34. modules.NewAnsibleManager(
  35. "ansibleplaybook",
  36. "ansibleplaybooks",
  37. []string{
  38. "id",
  39. "name",
  40. "status",
  41. "start_time",
  42. "end_time",
  43. },
  44. []string{},
  45. ),
  46. }
  47. modules.Register(&AnsiblePlaybooks)
  48. }
  49. func (man *AnsiblePlaybookManager) UpdateOrCreatePbModel(
  50. ctx context.Context,
  51. cliSess *mcclient.ClientSession,
  52. pbId string,
  53. pbName string,
  54. pb *ansible.Playbook,
  55. ) (*mcclient_models.AnsiblePlaybook, error) {
  56. if pbId == "" {
  57. pbJson, err := man.Get(cliSess, pbName, nil)
  58. if err == nil {
  59. pbModel := &mcclient_models.AnsiblePlaybook{}
  60. if err := pbJson.Unmarshal(pbModel); err == nil {
  61. pbId = pbModel.Id
  62. }
  63. }
  64. }
  65. var pbJson jsonutils.JSONObject
  66. if pbId != "" {
  67. var err error
  68. ansiblePbInput := &ansible_apis.AnsiblePlaybookUpdateInput{
  69. Name: pbName,
  70. Playbook: *pb,
  71. }
  72. params := ansiblePbInput.JSON(ansiblePbInput)
  73. pbJson, err = man.Update(cliSess, pbId, params)
  74. if err != nil {
  75. return nil, errors.Wrap(err, "update ansibleplaybook")
  76. }
  77. } else {
  78. var err error
  79. ansiblePbInput := &ansible_apis.AnsiblePlaybookCreateInput{
  80. Name: pbName,
  81. Playbook: *pb,
  82. }
  83. params := ansiblePbInput.JSON(ansiblePbInput)
  84. pbJson, err = man.Create(cliSess, params)
  85. if err != nil {
  86. return nil, errors.Wrap(err, "create ansibleplaybook")
  87. }
  88. }
  89. pbModel := &mcclient_models.AnsiblePlaybook{}
  90. if err := pbJson.Unmarshal(pbModel); err != nil {
  91. return nil, errors.Wrap(err, "unmarshal ansibleplaybook")
  92. }
  93. return pbModel, nil
  94. }