containers.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 cloudpods
  15. import (
  16. "time"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. )
  23. type SContainer struct {
  24. multicloud.SResourceBase
  25. CloudpodsTags
  26. region *SRegion
  27. api.SContainer
  28. }
  29. func (region *SRegion) GetContainers(guestId string) ([]SContainer, error) {
  30. ret := []SContainer{}
  31. params := map[string]interface{}{
  32. "guest_id": guestId,
  33. }
  34. err := region.list(&modules.Containers, params, &ret)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return ret, nil
  39. }
  40. func (container *SContainer) GetId() string {
  41. return container.Id
  42. }
  43. func (container *SContainer) GetGlobalId() string {
  44. return container.Id
  45. }
  46. func (container *SContainer) GetName() string {
  47. return container.Name
  48. }
  49. func (container *SContainer) GetStatus() string {
  50. return container.Status
  51. }
  52. func (container *SContainer) GetStartedAt() time.Time {
  53. return container.StartedAt
  54. }
  55. func (container *SContainer) GetLastFinishedAt() time.Time {
  56. return container.LastFinishedAt
  57. }
  58. func (container *SContainer) GetRestartCount() int {
  59. return container.RestartCount
  60. }
  61. func (container *SContainer) GetVolumentMounts() ([]cloudprovider.ICloudVolumeMount, error) {
  62. if container.Spec == nil {
  63. return []cloudprovider.ICloudVolumeMount{}, nil
  64. }
  65. ret := []cloudprovider.ICloudVolumeMount{}
  66. for id := range container.Spec.VolumeMounts {
  67. ret = append(ret, &SContainerVolumeMount{
  68. container.Spec.VolumeMounts[id],
  69. })
  70. }
  71. return ret, nil
  72. }
  73. func (container *SContainer) GetDevices() ([]cloudprovider.IContainerDevice, error) {
  74. if container.Spec == nil {
  75. return []cloudprovider.IContainerDevice{}, nil
  76. }
  77. ret := []cloudprovider.IContainerDevice{}
  78. for id := range container.Spec.Devices {
  79. ret = append(ret, &SContainerDevice{
  80. container.Spec.Devices[id],
  81. })
  82. }
  83. return ret, nil
  84. }
  85. type SContainerVolumeMount struct {
  86. *apis.ContainerVolumeMount
  87. }
  88. func (volumeMount *SContainerVolumeMount) GetName() string {
  89. return volumeMount.UniqueName
  90. }
  91. func (volumeMount *SContainerVolumeMount) IsReadOnly() bool {
  92. return volumeMount.ReadOnly
  93. }
  94. func (volumeMount *SContainerVolumeMount) GetType() string {
  95. return string(volumeMount.Type)
  96. }
  97. type SContainerDevice struct {
  98. *api.ContainerDevice
  99. }
  100. func (device *SContainerDevice) GetId() string {
  101. if device.IsolatedDevice != nil {
  102. return device.IsolatedDevice.Id
  103. }
  104. return ""
  105. }
  106. func (device *SContainerDevice) GetType() string {
  107. return string(device.Type)
  108. }
  109. func (container *SContainer) GetImage() string {
  110. if container.Spec == nil {
  111. return ""
  112. }
  113. return container.Spec.Image
  114. }
  115. func (container *SContainer) GetCommand() []string {
  116. if container.Spec == nil {
  117. return []string{}
  118. }
  119. return container.Spec.Command
  120. }
  121. func (container *SContainer) GetEnvs() []cloudprovider.SContainerEnv {
  122. if container.Spec == nil {
  123. return []cloudprovider.SContainerEnv{}
  124. }
  125. envs := make([]cloudprovider.SContainerEnv, 0)
  126. for _, env := range container.Spec.Envs {
  127. envs = append(envs, cloudprovider.SContainerEnv{
  128. Key: env.Key,
  129. Value: env.Value,
  130. })
  131. }
  132. return envs
  133. }