|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "google.golang.org/grpc" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + pprovisioning "github.com/onokatio/terraform-provider-n0stack/n0proto.go/provisioning/v0" |
| 8 | +) |
| 9 | + |
| 10 | +func resource_n0stack_virtualmachine() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Create: resource_n0stack_virtualmachine_create, |
| 13 | + Read: resource_n0stack_virtualmachine_read, |
| 14 | + Update: resource_n0stack_virtualmachine_update, |
| 15 | + Delete: resource_n0stack_virtualmachine_delete, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "name": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + }, |
| 22 | + "annotations": { |
| 23 | + Type: schema.TypeMap, |
| 24 | + Required: true, |
| 25 | + Elem: schema.TypeString, |
| 26 | + }, |
| 27 | + "labels": { |
| 28 | + Type: schema.TypeMap, |
| 29 | + Optional: true, |
| 30 | + Elem: schema.TypeString, |
| 31 | + }, |
| 32 | + "request_cpu_milli_core": { |
| 33 | + Type: schema.TypeInt, |
| 34 | + Required: true, |
| 35 | + }, |
| 36 | + "limit_cpu_milli_core": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Required: true, |
| 39 | + }, |
| 40 | + "request_memory_bytes": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + "limit_memory_bytes": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Required: true, |
| 47 | + }, |
| 48 | + "block_storage_names": { |
| 49 | + Type: schema.TypeList, |
| 50 | + Required: true, |
| 51 | + Elem: &schema.Schema{ |
| 52 | + Type: schema.TypeString, |
| 53 | + }, |
| 54 | + }, |
| 55 | + "nics": { |
| 56 | + Type: schema.TypeList, |
| 57 | + Required: true, |
| 58 | + Elem: &schema.Resource{ |
| 59 | + Schema: map[string]*schema.Schema{ |
| 60 | + "network_name": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Required: true, |
| 63 | + }, |
| 64 | + "ipv4_address": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Required: true, |
| 67 | + }, |
| 68 | + "ipv6_address": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func resource_n0stack_virtualmachine_create(d *schema.ResourceData, meta interface{}) error { |
| 80 | + config := meta.(Config) |
| 81 | + conn, err := grpc.Dial(config.endpoint, grpc.WithInsecure()) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer conn.Close() |
| 86 | + |
| 87 | + client := pprovisioning.NewVirtualMachineServiceClient(conn) |
| 88 | + |
| 89 | + nics := make([]*(pprovisioning.VirtualMachineNIC), len(d.Get("nics").([]interface{})) ) |
| 90 | + for _, value := range d.Get("nics").([]interface{}) { |
| 91 | + nic := pprovisioning.VirtualMachineNIC{} |
| 92 | + element := value.(map[string]interface{}) |
| 93 | + nic.NetworkName = element["network_name"].(string) |
| 94 | + nic.Ipv4Address = element["ipv4_address"].(string) |
| 95 | + nic.Ipv6Address = element["ipv6_address"].(string) |
| 96 | + nics = append(nics, &nic) |
| 97 | + } |
| 98 | + |
| 99 | + request := pprovisioning.CreateVirtualMachineRequest{ |
| 100 | + Name: d.Get("name").(string), |
| 101 | + Annotations: interfaceMap2stringMap(d.Get("annotations").(map[string]interface{})), |
| 102 | + Labels: interfaceMap2stringMap(d.Get("labels").(map[string]interface{})), |
| 103 | + RequestCpuMilliCore: uint32(d.Get("request_cpu_milli_core").(int)), |
| 104 | + LimitCpuMilliCore: uint32(d.Get("limit_cpu_milli_core").(int)), |
| 105 | + RequestMemoryBytes: uint64(d.Get("request_memory_bytes").(int)), |
| 106 | + LimitMemoryBytes: uint64(d.Get("limit_memory_bytes").(int)), |
| 107 | + BlockStorageNames: interfaceList2stringList(d.Get("block_storage_names").([]interface{})), |
| 108 | + Nics: nics, |
| 109 | + } |
| 110 | + _, err = client.CreateVirtualMachine(context.Background(), &request) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + d.SetId(d.Get("name").(string)) |
| 116 | + |
| 117 | + return resource_n0stack_virtualmachine_read(d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resource_n0stack_virtualmachine_read(d *schema.ResourceData, meta interface{}) error { |
| 121 | + config := meta.(Config) |
| 122 | + conn, err := grpc.Dial(config.endpoint, grpc.WithInsecure()) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + defer conn.Close() |
| 127 | + |
| 128 | + client := pprovisioning.NewVirtualMachineServiceClient(conn) |
| 129 | + |
| 130 | + request := pprovisioning.GetVirtualMachineRequest{ |
| 131 | + Name: d.Get("name").(string) , |
| 132 | + } |
| 133 | + res, err := client.GetVirtualMachine(context.Background(), &request) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + d.Set("name",res.Name) |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +func resource_n0stack_virtualmachine_update(d *schema.ResourceData, meta interface{}) error { |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func resource_n0stack_virtualmachine_delete(d *schema.ResourceData, meta interface{}) error { |
| 147 | + config := meta.(Config) |
| 148 | + conn, err := grpc.Dial(config.endpoint, grpc.WithInsecure()) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + defer conn.Close() |
| 153 | + |
| 154 | + client := pprovisioning.NewVirtualMachineServiceClient(conn) |
| 155 | + |
| 156 | + request := pprovisioning.DeleteVirtualMachineRequest{ |
| 157 | + Name: d.Get("name").(string) , |
| 158 | + } |
| 159 | + _, err = client.DeleteVirtualMachine(context.Background(), &request) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + return resource_n0stack_virtualmachine_read(d, meta) |
| 165 | +} |
0 commit comments