This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.go
121 lines (103 loc) · 2.76 KB
/
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
package main
import (
"context"
"google.golang.org/grpc"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
pdeployment "github.com/n0stack/n0stack/n0proto.go/deployment/v0"
)
func resource_n0stack_image() *schema.Resource {
return &schema.Resource{
Create: resource_n0stack_image_create,
Read: resource_n0stack_image_read,
Update: resource_n0stack_image_update,
Delete: resource_n0stack_image_delete,
Schema: map[string]*schema.Schema{
"image_name": {
Type: schema.TypeString,
Required: true,
},
"tags": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"blockstorage_name": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resource_n0stack_image_create(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
conn, err := grpc.Dial(config.endpoint, grpc.WithInsecure())
if err != nil {
return err
}
defer conn.Close()
client := pdeployment.NewImageServiceClient(conn)
{
request := pdeployment.ApplyImageRequest{
Name: d.Get("image_name").(string) ,
}
_, err = client.ApplyImage(context.Background(), &request)
if err != nil {
return err
}
}
{
request := pdeployment.RegisterBlockStorageRequest{
ImageName: d.Get("image_name").(string) ,
BlockStorageName: d.Get("blockstorage_name").(string),
Tags: interfaceList2stringList(d.Get("tags").([]interface{})),
}
_, err = client.RegisterBlockStorage(context.Background(), &request)
if err != nil {
return err
}
}
d.SetId(d.Get("image_name").(string))
return resource_n0stack_image_read(d, meta)
}
func resource_n0stack_image_read(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
conn, err := grpc.Dial(config.endpoint, grpc.WithInsecure())
if err != nil {
return err
}
defer conn.Close()
client := pdeployment.NewImageServiceClient(conn)
request := pdeployment.GetImageRequest{
Name: d.Get("image_name").(string) ,
}
res, err := client.GetImage(context.Background(), &request)
if err != nil {
return err
}
d.Set("image_name",res.Name)
d.Set("tags",res.Tags)
//d.Set("blockstorage_name",res.RegisteredBlockStorages)
return nil
}
func resource_n0stack_image_update(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resource_n0stack_image_delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
conn, err := grpc.Dial(config.endpoint, grpc.WithInsecure())
if err != nil {
return err
}
defer conn.Close()
client := pdeployment.NewImageServiceClient(conn)
request := pdeployment.DeleteImageRequest{
Name: d.Get("image_name").(string) ,
}
_, err = client.DeleteImage(context.Background(), &request)
if err != nil {
return err
}
return nil
}