-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_repository_nuget_group.go
136 lines (110 loc) · 4.2 KB
/
resource_repository_nuget_group.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
package repository
import (
"reflect"
"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"
)
func ResourceRepositoryNugetGroup() *schema.Resource {
return &schema.Resource{
Description: "Use this resource to create a group nuget repository.",
Create: resourceNugetGroupRepositoryCreate,
Delete: resourceNugetGroupRepositoryDelete,
Exists: resourceNugetGroupRepositoryExists,
Read: resourceNugetGroupRepositoryRead,
Update: resourceNugetGroupRepositoryUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
// Common schemas
"id": common.ResourceID,
"name": repositorySchema.ResourceName,
"online": repositorySchema.ResourceOnline,
// Group schemas
"group": repositorySchema.ResourceGroup,
"storage": repositorySchema.ResourceStorage,
},
}
}
func getNugetGroupRepositoryFromResourceData(resourceData *schema.ResourceData) repository.NugetGroupRepository {
storageConfig := resourceData.Get("storage").([]interface{})[0].(map[string]interface{})
groupConfig := resourceData.Get("group").([]interface{})[0].(map[string]interface{})
groupMemberNamesInterface := groupConfig["member_names"].([]interface{})
groupMemberNames := make([]string, 0)
for _, v := range groupMemberNamesInterface {
groupMemberNames = append(groupMemberNames, v.(string))
}
repo := repository.NugetGroupRepository{
Name: resourceData.Get("name").(string),
Online: resourceData.Get("online").(bool),
Storage: repository.Storage{
BlobStoreName: storageConfig["blob_store_name"].(string),
StrictContentTypeValidation: storageConfig["strict_content_type_validation"].(bool),
},
Group: repository.Group{
MemberNames: groupMemberNames,
},
}
return repo
}
func setNugetGroupRepositoryToResourceData(repo *repository.NugetGroupRepository, resourceData *schema.ResourceData) error {
resourceData.SetId(repo.Name)
resourceData.Set("name", repo.Name)
resourceData.Set("online", repo.Online)
if err := resourceData.Set("storage", flattenStorage(&repo.Storage)); err != nil {
return err
}
if err := resourceData.Set("group", flattenGroup(&repo.Group)); err != nil {
return err
}
return nil
}
func resourceNugetGroupRepositoryCreate(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
repo := getNugetGroupRepositoryFromResourceData(resourceData)
if err := client.Repository.Nuget.Group.Create(repo); err != nil {
return err
}
resourceData.SetId(repo.Name)
return resourceNugetGroupRepositoryRead(resourceData, m)
}
func resourceNugetGroupRepositoryRead(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
repo, err := client.Repository.Nuget.Group.Get(resourceData.Id())
if err != nil {
return err
}
if repo == nil {
resourceData.SetId("")
return nil
}
return setNugetGroupRepositoryToResourceData(repo, resourceData)
}
func resourceNugetGroupRepositoryUpdate(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
repoName := resourceData.Id()
repo := getNugetGroupRepositoryFromResourceData(resourceData)
repo1, err := client.Repository.Nuget.Group.Get(resourceData.Id())
if err != nil {
return err
}
if reflect.DeepEqual(repo1.Group.MemberNames, repo.Group.MemberNames) {
return nil
}
if err := client.Repository.Nuget.Group.Update(repoName, repo); err != nil {
return err
}
return resourceNugetGroupRepositoryRead(resourceData, m)
}
func resourceNugetGroupRepositoryDelete(resourceData *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
return client.Repository.Nuget.Group.Delete(resourceData.Id())
}
func resourceNugetGroupRepositoryExists(resourceData *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*nexus.NexusClient)
repo, err := client.Repository.Nuget.Group.Get(resourceData.Id())
return repo != nil, err
}