This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from sardinasystems/fleeting-plugin-openstack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
124 lines (98 loc) · 2.7 KB
/
utils.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
package fpoc
import (
"maps"
"regexp"
"strings"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/mitchellh/mapstructure"
)
// ExtCreateOpts extended version of servers.CreateOpts
// nolint:revive
type ExtCreateOpts struct {
servers.CreateOpts
// fields absent in gophercloud
Description string `json:"description,omitempty"`
KeyName string `json:"key_name,omitempty"`
// annotation overrides
Networks []servers.Network `json:"networks,omitempty"`
SecurityGroups []string `json:"security_groups,omitempty"`
UserData string `json:"user_data,omitempty"`
SchedulerHints *servers.SchedulerHints `json:"scheduler_hints,omitempty"`
}
// ToServerCreateMap for extended opts
func (opts ExtCreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
if opts.Networks != nil {
opts.CreateOpts.Networks = opts.Networks
}
if opts.SecurityGroups != nil {
opts.CreateOpts.SecurityGroups = opts.SecurityGroups
}
if opts.UserData != "" {
opts.CreateOpts.UserData = []byte(opts.UserData)
}
if opts.SchedulerHints != nil {
opts.CreateOpts.SchedulerHints = opts.SchedulerHints
}
ob, err := opts.CreateOpts.ToServerCreateMap()
if err != nil {
return nil, err
}
/*
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
delete(b, "user_data")
delete(b, "security_groups")
delete(b, "SchedulerHints")
*/
b := map[string]any{}
if opts.Description != "" {
b["description"] = opts.Description
}
if opts.KeyName != "" {
b["key_name"] = opts.KeyName
}
sob := ob["server"].(map[string]any)
maps.Copy(sob, b)
return ob, nil
}
type Address struct {
Version int `json:"version"`
Address string `json:"addr"`
MACAddr string `json:"OS-EXT-IPS-MAC:mac_addr,omitempty"`
Type string `json:"OS-EXT-IPS:type,omitempty"`
}
func extractAddresses(srv *servers.Server) (map[string][]Address, error) {
ret := make(map[string][]Address, len(srv.Addresses))
for net, isv := range srv.Addresses {
ism := isv.([]interface{})
items := make([]Address, 0, len(ism))
for _, iv := range ism {
var out Address
cfg := &mapstructure.DecoderConfig{
Metadata: nil,
Result: &out,
TagName: "json",
}
decoder, _ := mapstructure.NewDecoder(cfg)
err := decoder.Decode(iv)
if err != nil {
return nil, err
}
items = append(items, out)
}
ret[net] = items
}
return ret, nil
}
var initFinishedRe = regexp.MustCompile(`^.*Cloud-init\ v\.\ \d+\.\d+\.\d+\ finished\ at.*$`)
func IsCloudInitFinished(log string) bool {
lines := strings.Split(log, "\n")
for _, line := range lines {
if initFinishedRe.MatchString(line) {
return true
}
}
return false
}