|
| 1 | +// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package oci |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 11 | + oci_artifacts "github.com/oracle/oci-go-sdk/v37/artifacts" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + RegisterDatasource("oci_artifacts_container_image", ArtifactsContainerImageDataSource()) |
| 16 | +} |
| 17 | + |
| 18 | +func ArtifactsContainerImageDataSource() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Read: readSingularArtifactsContainerImage, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "image_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + // Computed |
| 27 | + "compartment_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "created_by": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "digest": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "display_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "layers": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Computed: true, |
| 46 | + Elem: &schema.Resource{ |
| 47 | + Schema: map[string]*schema.Schema{ |
| 48 | + // Required |
| 49 | + |
| 50 | + // Optional |
| 51 | + |
| 52 | + // Computed |
| 53 | + "digest": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "size_in_bytes": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + }, |
| 61 | + "time_created": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + "layers_size_in_bytes": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "manifest_size_in_bytes": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "pull_count": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + }, |
| 80 | + "repository_id": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "repository_name": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "state": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + "time_created": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "time_last_pulled": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Computed: true, |
| 99 | + }, |
| 100 | + "version": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + }, |
| 104 | + "versions": { |
| 105 | + Type: schema.TypeList, |
| 106 | + Computed: true, |
| 107 | + Elem: &schema.Resource{ |
| 108 | + Schema: map[string]*schema.Schema{ |
| 109 | + // Required |
| 110 | + |
| 111 | + // Optional |
| 112 | + |
| 113 | + // Computed |
| 114 | + "created_by": { |
| 115 | + Type: schema.TypeString, |
| 116 | + Computed: true, |
| 117 | + }, |
| 118 | + "time_created": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Computed: true, |
| 121 | + }, |
| 122 | + "version": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func readSingularArtifactsContainerImage(d *schema.ResourceData, m interface{}) error { |
| 134 | + sync := &ArtifactsContainerImageDataSourceCrud{} |
| 135 | + sync.D = d |
| 136 | + sync.Client = m.(*OracleClients).artifactsClient() |
| 137 | + |
| 138 | + return ReadResource(sync) |
| 139 | +} |
| 140 | + |
| 141 | +type ArtifactsContainerImageDataSourceCrud struct { |
| 142 | + D *schema.ResourceData |
| 143 | + Client *oci_artifacts.ArtifactsClient |
| 144 | + Res *oci_artifacts.GetContainerImageResponse |
| 145 | +} |
| 146 | + |
| 147 | +func (s *ArtifactsContainerImageDataSourceCrud) VoidState() { |
| 148 | + s.D.SetId("") |
| 149 | +} |
| 150 | + |
| 151 | +func (s *ArtifactsContainerImageDataSourceCrud) Get() error { |
| 152 | + request := oci_artifacts.GetContainerImageRequest{} |
| 153 | + |
| 154 | + if imageId, ok := s.D.GetOkExists("image_id"); ok { |
| 155 | + tmp := imageId.(string) |
| 156 | + request.ImageId = &tmp |
| 157 | + } |
| 158 | + |
| 159 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "artifacts") |
| 160 | + |
| 161 | + response, err := s.Client.GetContainerImage(context.Background(), request) |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + |
| 166 | + s.Res = &response |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func (s *ArtifactsContainerImageDataSourceCrud) SetData() error { |
| 171 | + if s.Res == nil { |
| 172 | + return nil |
| 173 | + } |
| 174 | + |
| 175 | + s.D.SetId(*s.Res.Id) |
| 176 | + |
| 177 | + if s.Res.CompartmentId != nil { |
| 178 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 179 | + } |
| 180 | + |
| 181 | + if s.Res.CreatedBy != nil { |
| 182 | + s.D.Set("created_by", *s.Res.CreatedBy) |
| 183 | + } |
| 184 | + |
| 185 | + if s.Res.Digest != nil { |
| 186 | + s.D.Set("digest", *s.Res.Digest) |
| 187 | + } |
| 188 | + |
| 189 | + if s.Res.DisplayName != nil { |
| 190 | + s.D.Set("display_name", *s.Res.DisplayName) |
| 191 | + } |
| 192 | + |
| 193 | + layers := []interface{}{} |
| 194 | + for _, item := range s.Res.Layers { |
| 195 | + layers = append(layers, ContainerImageLayerToMap(item)) |
| 196 | + } |
| 197 | + s.D.Set("layers", layers) |
| 198 | + |
| 199 | + if s.Res.LayersSizeInBytes != nil { |
| 200 | + s.D.Set("layers_size_in_bytes", strconv.FormatInt(*s.Res.LayersSizeInBytes, 10)) |
| 201 | + } |
| 202 | + |
| 203 | + if s.Res.ManifestSizeInBytes != nil { |
| 204 | + s.D.Set("manifest_size_in_bytes", *s.Res.ManifestSizeInBytes) |
| 205 | + } |
| 206 | + |
| 207 | + if s.Res.PullCount != nil { |
| 208 | + s.D.Set("pull_count", strconv.FormatInt(*s.Res.PullCount, 10)) |
| 209 | + } |
| 210 | + |
| 211 | + if s.Res.RepositoryId != nil { |
| 212 | + s.D.Set("repository_id", *s.Res.RepositoryId) |
| 213 | + } |
| 214 | + |
| 215 | + if s.Res.RepositoryName != nil { |
| 216 | + s.D.Set("repository_name", *s.Res.RepositoryName) |
| 217 | + } |
| 218 | + |
| 219 | + s.D.Set("state", s.Res.LifecycleState) |
| 220 | + |
| 221 | + if s.Res.TimeCreated != nil { |
| 222 | + s.D.Set("time_created", s.Res.TimeCreated.String()) |
| 223 | + } |
| 224 | + |
| 225 | + if s.Res.TimeLastPulled != nil { |
| 226 | + s.D.Set("time_last_pulled", s.Res.TimeLastPulled.String()) |
| 227 | + } |
| 228 | + |
| 229 | + if s.Res.Version != nil { |
| 230 | + s.D.Set("version", *s.Res.Version) |
| 231 | + } |
| 232 | + |
| 233 | + versions := []interface{}{} |
| 234 | + for _, item := range s.Res.Versions { |
| 235 | + versions = append(versions, ContainerVersionToMap(item)) |
| 236 | + } |
| 237 | + s.D.Set("versions", versions) |
| 238 | + |
| 239 | + return nil |
| 240 | +} |
| 241 | + |
| 242 | +func ContainerImageLayerToMap(obj oci_artifacts.ContainerImageLayer) map[string]interface{} { |
| 243 | + result := map[string]interface{}{} |
| 244 | + |
| 245 | + if obj.Digest != nil { |
| 246 | + result["digest"] = string(*obj.Digest) |
| 247 | + } |
| 248 | + |
| 249 | + if obj.SizeInBytes != nil { |
| 250 | + result["size_in_bytes"] = strconv.FormatInt(*obj.SizeInBytes, 10) |
| 251 | + } |
| 252 | + |
| 253 | + if obj.TimeCreated != nil { |
| 254 | + result["time_created"] = obj.TimeCreated.String() |
| 255 | + } |
| 256 | + |
| 257 | + return result |
| 258 | +} |
| 259 | + |
| 260 | +func ContainerImageSummaryToMap(obj oci_artifacts.ContainerImageSummary) map[string]interface{} { |
| 261 | + result := map[string]interface{}{} |
| 262 | + |
| 263 | + if obj.CompartmentId != nil { |
| 264 | + result["compartment_id"] = string(*obj.CompartmentId) |
| 265 | + } |
| 266 | + |
| 267 | + if obj.Digest != nil { |
| 268 | + result["digest"] = string(*obj.Digest) |
| 269 | + } |
| 270 | + |
| 271 | + if obj.DisplayName != nil { |
| 272 | + result["display_name"] = string(*obj.DisplayName) |
| 273 | + } |
| 274 | + |
| 275 | + if obj.Id != nil { |
| 276 | + result["id"] = string(*obj.Id) |
| 277 | + } |
| 278 | + |
| 279 | + if obj.RepositoryId != nil { |
| 280 | + result["repository_id"] = string(*obj.RepositoryId) |
| 281 | + } |
| 282 | + |
| 283 | + if obj.RepositoryName != nil { |
| 284 | + result["repository_name"] = string(*obj.RepositoryName) |
| 285 | + } |
| 286 | + |
| 287 | + result["state"] = string(obj.LifecycleState) |
| 288 | + |
| 289 | + if obj.TimeCreated != nil { |
| 290 | + result["time_created"] = obj.TimeCreated.String() |
| 291 | + } |
| 292 | + |
| 293 | + if obj.Version != nil { |
| 294 | + result["version"] = string(*obj.Version) |
| 295 | + } |
| 296 | + |
| 297 | + return result |
| 298 | +} |
| 299 | + |
| 300 | +func ContainerVersionToMap(obj oci_artifacts.ContainerVersion) map[string]interface{} { |
| 301 | + result := map[string]interface{}{} |
| 302 | + |
| 303 | + if obj.CreatedBy != nil { |
| 304 | + result["created_by"] = string(*obj.CreatedBy) |
| 305 | + } |
| 306 | + |
| 307 | + if obj.TimeCreated != nil { |
| 308 | + result["time_created"] = obj.TimeCreated.String() |
| 309 | + } |
| 310 | + |
| 311 | + if obj.Version != nil { |
| 312 | + result["version"] = string(*obj.Version) |
| 313 | + } |
| 314 | + |
| 315 | + return result |
| 316 | +} |
0 commit comments