-
Notifications
You must be signed in to change notification settings - Fork 16
/
incus_import.go
166 lines (137 loc) · 4.3 KB
/
incus_import.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package common
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/lxc/terraform-provider-incus/internal/utils"
)
// ImportMetadata defines import ID properties that are used when parsing
// values from an ID.
type ImportMetadata struct {
ResourceName string
RequiredFields []string
AllowedOptions []string
}
// ParseImportID parses remote name, project name, required fields, and other
// allowed options from an import ID.
//
// Remote is separated using colun "[remote:]". Project and other required
// fields are separated using slash "[project/]rf". If there are multiple
// required fields, first slash becomes mandatory "[project]/rf1/rf2".
// Options are separated using comma "rf[,opt1=value][,opt2=value]".
//
// Expected format:
//
// [remote:][project/]name[,optKey1=optVal1][,optKeyN=optValN]
//
// Result is a map of key=value pairs:
//
// map[string]string{
// "remote": "local"
// "project": "test"
// "name": "myres"
// "image": "alpine" // option
// "optKey2": "value" // option
// }
func (m ImportMetadata) ParseImportID(importId string) (map[string]string, diag.Diagnostic) {
if strings.TrimSpace(importId) == "" {
return nil, newImportIDError(m, importId, fmt.Errorf("Import ID cannot be empty"))
}
// First split by comma to determine mandatory and optional parts.
parts := strings.Split(importId, ",")
// Extract fields (including project and remote) from first part.
result, err := processFields(parts[0], m.RequiredFields)
if err != nil {
return nil, newImportIDError(m, importId, err)
}
// Extract options.
if len(parts) > 1 {
options, err := processOptions(parts[1:], m.AllowedOptions)
if err != nil {
return nil, newImportIDError(m, importId, err)
}
for k, v := range options {
result[k] = v
}
}
return result, nil
}
// processFields convert the mandatory part of the import ID into remote,
// project, and any number of provided required fields.
func processFields(id string, requiredFields []string) (map[string]string, error) {
result := make(map[string]string)
// Split id into [remote:]<id>
parts := strings.SplitN(id, ":", 2)
if len(parts) > 1 {
remote := parts[0]
if remote != "" {
result["remote"] = remote
}
id = parts[1]
}
// Split the remaining id into project and required fields.
parts = strings.Split(id, "/")
if len(parts) > 1 {
project := parts[0]
if project != "" {
result["project"] = project
}
parts = parts[1:]
}
// Ensure the length of remaining fields equals the length of
// required fields.
if len(parts) != len(requiredFields) {
return nil, fmt.Errorf("Import ID does not contain all required fields: [%v]", strings.Join(requiredFields, ", "))
}
// Extract values for fields into result.
for i := range parts {
key := requiredFields[i]
val := parts[i]
if val == "" {
return nil, fmt.Errorf("Import ID requires non-empty value for %q", key)
}
result[key] = val
}
return result, nil
}
// processOptions convert optional part of the import id and returns it as
// a map. If non-allowed or empty option is extracted, an error is returned.
func processOptions(options []string, allowedOptions []string) (map[string]string, error) {
result := make(map[string]string)
for _, o := range options {
if o == "" {
continue
}
parts := strings.Split(o, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("Import ID contains invalid option %q. Options must be in key=value format", o)
}
key := parts[0]
val := parts[1]
if !utils.ValueInSlice(key, allowedOptions) {
return nil, fmt.Errorf("Import ID contains unexpected option %q", key)
}
result[key] = val
}
return result, nil
}
// newImportIDError converts an error into terraform diagnostic.
func newImportIDError(m ImportMetadata, importId string, err error) diag.Diagnostic {
remote := "[<remote>:]"
project := "[<project>/]"
if len(m.RequiredFields) > 1 {
project = "[<project>]/"
}
options := ""
for _, o := range m.AllowedOptions {
options += fmt.Sprintf("[,%s=<value>]", o)
}
fields := fmt.Sprintf("<%s>", strings.Join(m.RequiredFields, ">/<"))
return diag.NewErrorDiagnostic(
fmt.Sprintf("Invalid import ID: %q", importId),
fmt.Sprintf(
"%v.\n\nValid import format:\nimport incus_%s.<resource> %s%s%s%s",
err, m.ResourceName, remote, project, fields, options,
),
)
}