-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_image.go
197 lines (177 loc) · 5.12 KB
/
data_source_ibm_is_image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMISImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISImageRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"identifier", "name"},
Description: "Image name",
},
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"identifier", "name"},
Description: "Image id",
},
"visibility": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "Whether the image is publicly visible or private to the account",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of this image",
},
"os": {
Type: schema.TypeString,
Computed: true,
Description: "Image Operating system",
},
"architecture": {
Type: schema.TypeString,
Computed: true,
Description: "The operating system architecture",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this image",
},
isImageCheckSum: {
Type: schema.TypeString,
Computed: true,
Description: "The SHA256 Checksum for this image",
},
isImageEncryptionKey: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN of the Key Protect Root Key or Hyper Protect Crypto Service Root Key for this resource",
},
isImageEncryption: {
Type: schema.TypeString,
Computed: true,
Description: "The type of encryption used on the image",
},
"source_volume": {
Type: schema.TypeString,
Computed: true,
Description: "Source volume id of the image",
},
},
}
}
func dataSourceIBMISImageRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
identifier := d.Get("identifier").(string)
var visibility string
if v, ok := d.GetOk("visibility"); ok {
visibility = v.(string)
}
if name != "" {
err := imageGetByName(d, meta, name, visibility)
if err != nil {
return err
}
} else if identifier != "" {
err := imageGetById(d, meta, identifier)
if err != nil {
return err
}
}
return nil
}
func imageGetByName(d *schema.ResourceData, meta interface{}, name, visibility string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
listImagesOptions := &vpcv1.ListImagesOptions{
Name: &name,
}
if visibility != "" {
listImagesOptions.Visibility = &visibility
}
availableImages, response, err := sess.ListImages(listImagesOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Images %s\n%s", err, response)
}
allrecs := availableImages.Images
if len(allrecs) == 0 {
return fmt.Errorf("[ERROR] No image found with name %s", name)
}
image := allrecs[0]
d.SetId(*image.ID)
d.Set("status", *image.Status)
if *image.Status == "deprecated" {
fmt.Printf("[WARN] Given image %s is deprecated and soon will be obsolete.", name)
}
d.Set("name", *image.Name)
d.Set("visibility", *image.Visibility)
d.Set("os", *image.OperatingSystem.Name)
d.Set("architecture", *image.OperatingSystem.Architecture)
d.Set("crn", *image.CRN)
if image.Encryption != nil {
d.Set("encryption", *image.Encryption)
}
if image.EncryptionKey != nil {
d.Set("encryption_key", *image.EncryptionKey.CRN)
}
if image.File != nil && image.File.Checksums != nil {
d.Set(isImageCheckSum, *image.File.Checksums.Sha256)
}
if image.SourceVolume != nil {
d.Set("source_volume", *image.SourceVolume.ID)
}
return nil
}
func imageGetById(d *schema.ResourceData, meta interface{}, identifier string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getImageOptions := &vpcv1.GetImageOptions{
ID: &identifier,
}
image, response, err := sess.GetImage(getImageOptions)
if err != nil {
if response.StatusCode == 404 {
return fmt.Errorf("[ERROR] No image found with id %s", identifier)
}
return fmt.Errorf("[ERROR] Error Fetching Images %s\n%s", err, response)
}
d.SetId(*image.ID)
d.Set("status", *image.Status)
if *image.Status == "deprecated" {
fmt.Printf("[WARN] Given image %s is deprecated and soon will be obsolete.", name)
}
d.Set("name", *image.Name)
d.Set("visibility", *image.Visibility)
d.Set("os", *image.OperatingSystem.Name)
d.Set("architecture", *image.OperatingSystem.Architecture)
d.Set("crn", *image.CRN)
if image.Encryption != nil {
d.Set("encryption", *image.Encryption)
}
if image.EncryptionKey != nil {
d.Set("encryption_key", *image.EncryptionKey.CRN)
}
if image.File != nil && image.File.Checksums != nil {
d.Set(isImageCheckSum, *image.File.Checksums.Sha256)
}
if image.SourceVolume != nil {
d.Set("source_volume", *image.SourceVolume.ID)
}
return nil
}