image_properties.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. )
  21. // +onecloud:swagger-gen-ignore
  22. type SImagePropertyManager struct {
  23. db.SResourceBaseManager
  24. }
  25. var ImagePropertyManager *SImagePropertyManager
  26. func init() {
  27. ImagePropertyManager = &SImagePropertyManager{
  28. SResourceBaseManager: db.NewResourceBaseManager(
  29. SImageProperty{},
  30. "image_properties",
  31. "image_property",
  32. "image_properties",
  33. ),
  34. }
  35. ImagePropertyManager.SetVirtualObject(ImagePropertyManager)
  36. ImagePropertyManager.TableSpec().AddIndex(true, "image_id", "name")
  37. }
  38. /*
  39. +------------+--------------+------+-----+---------+----------------+
  40. | Field | Type | Null | Key | Default | Extra |
  41. +------------+--------------+------+-----+---------+----------------+
  42. | id | int(11) | NO | PRI | NULL | auto_increment |
  43. | image_id | varchar(36) | NO | MUL | NULL | |
  44. | name | varchar(255) | NO | | NULL | |
  45. | value | text | YES | | NULL | |
  46. | created_at | datetime | NO | | NULL | |
  47. | updated_at | datetime | YES | | NULL | |
  48. | deleted_at | datetime | YES | | NULL | |
  49. | deleted | tinyint(1) | NO | MUL | NULL | |
  50. +------------+--------------+------+-----+---------+----------------+
  51. */
  52. // +onecloud:swagger-gen-ignore
  53. type SImageProperty struct {
  54. SImagePeripheral
  55. Name string `width:"255"`
  56. Value string `nullable:"true" create:"optional"`
  57. }
  58. func (manager *SImagePropertyManager) GetProperties(imageId string) (map[string]string, error) {
  59. properties := make([]SImageProperty, 0)
  60. q := manager.Query("name", "value").Equals("image_id", imageId)
  61. err := db.FetchModelObjects(manager, q, &properties)
  62. if err != nil {
  63. return nil, err
  64. }
  65. props := make(map[string]string)
  66. for i := range properties {
  67. props[properties[i].Name] = properties[i].Value
  68. }
  69. return props, nil
  70. }
  71. func (manager *SImagePropertyManager) SaveProperties(ctx context.Context, userCred mcclient.TokenCredential, imageId string, props jsonutils.JSONObject) error {
  72. propsJson := props.(*jsonutils.JSONDict)
  73. for _, k := range propsJson.SortedKeys() {
  74. v, _ := propsJson.GetString(k)
  75. _, err := manager.SaveProperty(ctx, userCred, imageId, k, v)
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. func (manager *SImagePropertyManager) SaveProperty(ctx context.Context, userCred mcclient.TokenCredential, imageId string, key string, value string) (*SImageProperty, error) {
  83. prop, _ := manager.GetProperty(imageId, key)
  84. if prop != nil {
  85. if prop.Value != value {
  86. return prop, prop.UpdateValue(ctx, userCred, value)
  87. } else {
  88. return prop, nil
  89. }
  90. } else {
  91. // create
  92. return manager.NewProperty(ctx, userCred, imageId, key, value)
  93. }
  94. }
  95. func (manager *SImagePropertyManager) GetProperty(imageId string, key string) (*SImageProperty, error) {
  96. q := manager.Query().Equals("image_id", imageId).Equals("name", key)
  97. prop := SImageProperty{}
  98. prop.SetModelManager(manager, &prop)
  99. err := q.First(&prop)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &prop, nil
  104. }
  105. func (manager *SImagePropertyManager) NewProperty(ctx context.Context, userCred mcclient.TokenCredential, imageId string, key string, value string) (*SImageProperty, error) {
  106. prop := SImageProperty{}
  107. prop.SetModelManager(manager, &prop)
  108. prop.ImageId = imageId
  109. prop.Name = key
  110. prop.Value = value
  111. err := manager.TableSpec().Insert(ctx, &prop)
  112. if err != nil {
  113. return nil, err
  114. }
  115. prop.SetModelManager(manager, &prop)
  116. return &prop, nil
  117. }
  118. func (self *SImageProperty) UpdateValue(ctx context.Context, userCred mcclient.TokenCredential, value string) error {
  119. _, err := db.Update(self, func() error {
  120. self.Value = value
  121. return nil
  122. })
  123. return err
  124. }