-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_repository_apt_hosted.go
194 lines (164 loc) · 6.02 KB
/
resource_repository_apt_hosted.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package repository
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
nexus "github.com/nduyphuong/go-nexus-client/nexus3"
"github.com/nduyphuong/go-nexus-client/nexus3/schema/repository"
"github.com/nduyphuong/terraform-provider-nexus/internal/schema/common"
repositorySchema "github.com/nduyphuong/terraform-provider-nexus/internal/schema/repository"
"github.com/nduyphuong/terraform-provider-nexus/internal/tools"
)
func ResourceRepositoryAptHosted() *schema.Resource {
return &schema.Resource{
Description: "Use this resource to create a hosted apt repository.",
Create: resourceAptHostedRepositoryCreate,
Delete: resourceAptHostedRepositoryDelete,
Exists: resourceAptHostedRepositoryExists,
Read: resourceAptHostedRepositoryRead,
Update: resourceAptHostedRepositoryUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
// Common schemas
"id": common.ResourceID,
"name": repositorySchema.ResourceName,
"online": repositorySchema.ResourceOnline,
// Hosted schemas
"cleanup": repositorySchema.ResourceCleanup,
"component": repositorySchema.ResourceComponent,
"storage": repositorySchema.ResourceHostedStorage,
// Apt hosted schemas
"distribution": {
Description: "Distribution to fetch",
Required: true,
Type: schema.TypeString,
},
"signing": {
Description: "Signing contains signing data of hosted repositores of format Apt",
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"keypair": {
Description: "PGP signing key pair (armored private key e.g. gpg --export-secret-key --armor)",
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"passphrase": {
Description: "Passphrase to access PGP signing key",
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
},
},
},
},
}
}
func getAptHostedRepositoryFromResourceData(resourceData *schema.ResourceData) repository.AptHostedRepository {
storageConfig := resourceData.Get("storage").([]interface{})[0].(map[string]interface{})
writePolicy := repository.StorageWritePolicy(storageConfig["write_policy"].(string))
signingConfig := resourceData.Get("signing").([]interface{})[0].(map[string]interface{})
repo := repository.AptHostedRepository{
Name: resourceData.Get("name").(string),
Online: resourceData.Get("online").(bool),
Storage: repository.HostedStorage{
BlobStoreName: storageConfig["blob_store_name"].(string),
StrictContentTypeValidation: storageConfig["strict_content_type_validation"].(bool),
WritePolicy: &writePolicy,
},
Apt: repository.AptHosted{
Distribution: resourceData.Get("distribution").(string),
},
AptSigning: repository.AptSigning{
Keypair: signingConfig["keypair"].(string),
},
}
if signingConfig["passphrase"] != nil {
repo.AptSigning.Passphrase = tools.GetStringPointer(signingConfig["passphrase"].(string))
}
cleanup, exists := resourceData.GetOk("cleanup")
if exists {
cleanupConfig := cleanup.([]interface{})[0].(map[string]interface{})
if len(cleanupConfig) > 0 {
policy_names, ok := cleanupConfig["policy_names"]
if ok {
repo.Cleanup = &repository.Cleanup{
PolicyNames: tools.InterfaceSliceToStringSlice(policy_names.(*schema.Set).List()),
}
}
}
}
componentList := resourceData.Get("component").([]interface{})
if len(componentList) > 0 && componentList[0] != nil {
componentConfig := componentList[0].(map[string]interface{})
if len(componentConfig) > 0 {
repo.Component = &repository.Component{
ProprietaryComponents: componentConfig["proprietary_components"].(bool),
}
}
}
return repo
}
func setAptHostedRepositoryToResourceData(repo *repository.AptHostedRepository, resourceData *schema.ResourceData) error {
resourceData.SetId(repo.Name)
resourceData.Set("name", repo.Name)
resourceData.Set("online", repo.Online)
resourceData.Set("distribution", repo.Apt.Distribution)
if err := resourceData.Set("storage", flattenHostedStorage(&repo.Storage)); err != nil {
return err
}
if repo.Cleanup != nil {
if err := resourceData.Set("cleanup", flattenCleanup(repo.Cleanup)); err != nil {
return err
}
}
if repo.Component != nil {
if err := resourceData.Set("component", flattenComponent(repo.Component)); err != nil {
return err
}
}
return nil
}
func resourceAptHostedRepositoryCreate(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
repo := getAptHostedRepositoryFromResourceData(resourceData)
if err := client.Repository.Apt.Hosted.Create(repo); err != nil {
return err
}
resourceData.SetId(repo.Name)
return resourceAptHostedRepositoryRead(resourceData, m)
}
func resourceAptHostedRepositoryRead(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
repo, err := client.Repository.Apt.Hosted.Get(resourceData.Id())
if err != nil {
return err
}
if repo == nil {
resourceData.SetId("")
return nil
}
return setAptHostedRepositoryToResourceData(repo, resourceData)
}
func resourceAptHostedRepositoryUpdate(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
repoName := resourceData.Id()
repo := getAptHostedRepositoryFromResourceData(resourceData)
if err := client.Repository.Apt.Hosted.Update(repoName, repo); err != nil {
return err
}
return resourceAptHostedRepositoryRead(resourceData, m)
}
func resourceAptHostedRepositoryDelete(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
return client.Repository.Apt.Hosted.Delete(resourceData.Id())
}
func resourceAptHostedRepositoryExists(resourceData *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*nexus.NexusClient)
repo, err := client.Repository.Apt.Hosted.Get(resourceData.Id())
return repo != nil, err
}