forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_container_registry_image.go
67 lines (63 loc) · 1.57 KB
/
data_source_container_registry_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
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceGoogleContainerImage() *schema.Resource {
return &schema.Resource{
Read: containerRegistryImageRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"tag": {
Type: schema.TypeString,
Optional: true,
},
"digest": {
Type: schema.TypeString,
Optional: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"image_url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func containerRegistryImageRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
d.Set("project", project)
region, ok := d.GetOk("region")
var url_base string
if ok && region != nil && region != "" {
url_base = fmt.Sprintf("%s.gcr.io/%s", region, project)
} else {
url_base = fmt.Sprintf("gcr.io/%s", project)
}
tag, t_ok := d.GetOk("tag")
digest, d_ok := d.GetOk("digest")
if t_ok && tag != nil && tag != "" {
d.Set("image_url", fmt.Sprintf("%s/%s:%s", url_base, d.Get("name").(string), tag))
} else if d_ok && digest != nil && digest != "" {
d.Set("image_url", fmt.Sprintf("%s/%s@%s", url_base, d.Get("name").(string), digest))
} else {
d.Set("image_url", fmt.Sprintf("%s/%s", url_base, d.Get("name").(string)))
}
d.SetId(d.Get("image_url").(string))
return nil
}