This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
forked from josenk/terraform-provider-esxi
/
resource-pool_create.go
139 lines (117 loc) · 3.7 KB
/
resource-pool_create.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
package esxi
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"strconv"
"strings"
)
func resourceRESOURCEPOOLCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*Config)
esxiSSHinfo := SshConnectionStruct{c.esxiHostName, c.esxiHostPort, c.esxiUserName, c.esxiPassword}
log.Println("[resourceRESOURCEPOOLCreate]")
var remote_cmd string
var pool_id, parent_pool string
var err error
resource_pool_name := d.Get("resource_pool_name").(string)
cpu_min := d.Get("cpu_min").(int)
cpu_min_expandable := d.Get("cpu_min_expandable").(string)
cpu_max := d.Get("cpu_max").(int)
cpu_shares := strings.ToLower(d.Get("cpu_shares").(string))
mem_min := d.Get("mem_min").(int)
mem_min_expandable := d.Get("mem_min_expandable").(string)
mem_max := d.Get("mem_max").(int)
mem_shares := strings.ToLower(d.Get("mem_shares").(string))
parent_pool = "Resources"
if resource_pool_name == string('/') {
return fmt.Errorf("Missing required resource_pool_name")
}
if resource_pool_name[0] == '/' {
return fmt.Errorf("Resource Pool Name cannot start with /")
}
i := strings.LastIndex(resource_pool_name, "/")
if i > 2 {
parent_pool = resource_pool_name[:i]
resource_pool_name = resource_pool_name[i+1:]
}
// Check if already exists
stdout, _ := getPoolID(c, resource_pool_name)
if stdout != "" {
d.SetId(stdout)
return nil
}
cpu_min_opt := "--cpu-min=0"
if cpu_min > 0 {
cpu_min_opt = fmt.Sprintf("--cpu-min=%d", cpu_min)
}
cpu_min_expandable_opt := "--cpu-min-expandable=true"
if cpu_min_expandable == "false" {
cpu_min_expandable_opt = "--cpu-min-expandable=false"
}
cpu_max_opt := ""
if cpu_max > 0 {
cpu_max_opt = fmt.Sprintf("--cpu-max=%d", cpu_max)
}
cpu_shares_opt := "--cpu-shares=normal"
if cpu_shares == "low" || cpu_shares == "high" {
cpu_shares_opt = fmt.Sprintf("--cpu-shares=%s", cpu_shares)
} else {
tmp_var, err := strconv.Atoi(cpu_shares)
if err == nil {
cpu_shares_opt = fmt.Sprintf("--cpu-shares=%d", tmp_var)
}
}
mem_min_opt := "--mem-min=0"
if mem_min > 0 {
mem_min_opt = fmt.Sprintf("--mem-min=%d", mem_min)
}
mem_min_expandable_opt := "--mem-min-expandable=true"
if mem_min_expandable == "false" {
mem_min_expandable_opt = "--mem-min-expandable=false"
}
mem_max_opt := ""
if mem_max > 0 {
mem_max_opt = fmt.Sprintf("--mem-max=%d", mem_max)
}
mem_shares_opt := "--mem-shares=normal"
if mem_shares == "low" || mem_shares == "high" {
mem_shares_opt = fmt.Sprintf("--mem-shares=%s", mem_shares)
} else {
tmp_var, err := strconv.Atoi(mem_shares)
if err == nil {
mem_shares_opt = fmt.Sprintf("--mem-shares=%d", tmp_var)
}
}
parent_pool_id, err := getPoolID(c, parent_pool)
if err != nil {
d.SetId("")
return err
}
remote_cmd = fmt.Sprintf("vim-cmd hostsvc/rsrc/create %s %s %s %s %s %s %s %s %s %s",
cpu_min_opt, cpu_min_expandable_opt, cpu_max_opt, cpu_shares_opt,
mem_min_opt, mem_min_expandable_opt, mem_max_opt, mem_shares_opt, parent_pool_id, resource_pool_name)
_, err = runRemoteSshCommand(esxiSSHinfo, remote_cmd, "create resource pool")
pool_id, _ = getPoolID(c, resource_pool_name)
if err != nil {
d.SetId("")
return err
}
// Set pool_id
d.SetId(pool_id)
// Refresh
resource_pool_name, cpu_min, cpu_min_expandable, cpu_max, cpu_shares, mem_min, mem_min_expandable, mem_max, mem_shares, err = resourcePoolRead(c, pool_id)
if err != nil {
d.SetId("")
return nil
}
d.Set("resource_pool_name", resource_pool_name)
d.Set("cpu_min", cpu_min)
d.Set("cpu_min_expandable", cpu_min_expandable)
d.Set("cpu_max", cpu_max)
d.Set("cpu_shares", cpu_shares)
d.Set("mem_min", mem_min)
d.Set("mem_min_expandable", mem_min_expandable)
d.Set("mem_max", mem_max)
d.Set("mem_shares", mem_shares)
return nil
}