Skip to content

Commit 534e23d

Browse files
author
Kalarrs Topham
authored
Enums instead of strings (#279)
* use struct string like enums. remove AccountLicenseType & convertVisibility as this can be done implicitly. fix func name. add enums for RepoType, RepoInitType * remove duplicate for set path * fix merge issue * use enum for BitBucket * use strings.EqualFold for string compare
1 parent 7a1b52b commit 534e23d

9 files changed

+103
-104
lines changed

azuredevops/data_projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func dataProjects() *schema.Resource {
3232
Type: schema.TypeString,
3333
ForceNew: true,
3434
Optional: true,
35-
Default: "all",
35+
Default: core.ProjectStateValues.All,
3636
ValidateFunc: validation.StringInSlice([]string{
3737
string(core.ProjectStateValues.Deleting),
3838
string(core.ProjectStateValues.New),

azuredevops/resource_agentpool.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ func resourceAzureAgentPool() *schema.Resource {
3030
ValidateFunc: validate.NoEmptyStrings,
3131
},
3232
"pool_type": {
33-
Type: schema.TypeString,
34-
Optional: true,
35-
ForceNew: true,
36-
Default: taskagent.TaskAgentPoolTypeValues.Automation,
37-
ValidateFunc: validation.StringInSlice([]string{string(taskagent.TaskAgentPoolTypeValues.Automation), string(taskagent.TaskAgentPoolTypeValues.Deployment)}, false),
33+
Type: schema.TypeString,
34+
Optional: true,
35+
ForceNew: true,
36+
Default: taskagent.TaskAgentPoolTypeValues.Automation,
37+
ValidateFunc: validation.StringInSlice([]string{
38+
string(taskagent.TaskAgentPoolTypeValues.Automation),
39+
string(taskagent.TaskAgentPoolTypeValues.Deployment),
40+
}, false),
3841
},
3942
"auto_provision": {
4043
Type: schema.TypeBool,

azuredevops/resource_build_definition.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import (
1616
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils/validate"
1717
)
1818

19+
// RepoType the type of the repository
20+
type RepoType string
21+
22+
type repoTypeValuesType struct {
23+
GitHub RepoType
24+
TfsGit RepoType
25+
Bitbucket RepoType
26+
}
27+
28+
// RepoTypeValues enum of the type of the repository
29+
var RepoTypeValues = repoTypeValuesType{
30+
GitHub: "GitHub",
31+
TfsGit: "TfsGit",
32+
Bitbucket: "Bitbucket",
33+
}
34+
1935
func resourceBuildDefinition() *schema.Resource {
2036
filterSchema := map[string]*schema.Schema{
2137
"include": {
@@ -62,7 +78,7 @@ func resourceBuildDefinition() *schema.Resource {
6278
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
6379
projectID, buildDefinitionID, err := ParseImportedProjectIDAndID(meta.(*config.AggregatedClient), d.Id())
6480
if err != nil {
65-
return nil, fmt.Errorf("Error parsing the build definition ID from the Terraform resource data: %v", err)
81+
return nil, fmt.Errorf("error parsing the build definition ID from the Terraform resource data: %v", err)
6682
}
6783
d.Set("project_id", projectID)
6884
d.SetId(fmt.Sprintf("%d", buildDefinitionID))
@@ -121,9 +137,13 @@ func resourceBuildDefinition() *schema.Resource {
121137
Required: true,
122138
},
123139
"repo_type": {
124-
Type: schema.TypeString,
125-
Required: true,
126-
ValidateFunc: validation.StringInSlice([]string{"GitHub", "TfsGit", "Bitbucket"}, false),
140+
Type: schema.TypeString,
141+
Required: true,
142+
ValidateFunc: validation.StringInSlice([]string{
143+
string(RepoTypeValues.GitHub),
144+
string(RepoTypeValues.TfsGit),
145+
string(RepoTypeValues.Bitbucket),
146+
}, false),
127147
},
128148
"branch_name": {
129149
Type: schema.TypeString,
@@ -257,12 +277,12 @@ func resourceBuildDefinitionCreate(d *schema.ResourceData, m interface{}) error
257277
}
258278
buildDefinition, projectID, err := expandBuildDefinition(d)
259279
if err != nil {
260-
return fmt.Errorf("Error creating resource Build Definition: %+v", err)
280+
return fmt.Errorf("error creating resource Build Definition: %+v", err)
261281
}
262282

263283
createdBuildDefinition, err := createBuildDefinition(clients, buildDefinition, projectID)
264284
if err != nil {
265-
return fmt.Errorf("Error creating resource Build Definition: %+v", err)
285+
return fmt.Errorf("error creating resource Build Definition: %+v", err)
266286
}
267287

268288
flattenBuildDefinition(d, createdBuildDefinition, projectID)
@@ -282,10 +302,10 @@ func flattenBuildDefinition(d *schema.ResourceData, buildDefinition *build.Build
282302

283303
if buildDefinition.Triggers != nil {
284304
yamlCiTrigger := hasSettingsSourceType(buildDefinition.Triggers, build.DefinitionTriggerTypeValues.ContinuousIntegration, 2)
285-
d.Set("ci_trigger", flattenReleaseDefinitionTriggers(buildDefinition.Triggers, yamlCiTrigger, build.DefinitionTriggerTypeValues.ContinuousIntegration))
305+
d.Set("ci_trigger", flattenBuildDefinitionTriggers(buildDefinition.Triggers, yamlCiTrigger, build.DefinitionTriggerTypeValues.ContinuousIntegration))
286306

287307
yamlPrTrigger := hasSettingsSourceType(buildDefinition.Triggers, build.DefinitionTriggerTypeValues.PullRequest, 2)
288-
d.Set("pull_request_trigger", flattenReleaseDefinitionTriggers(buildDefinition.Triggers, yamlPrTrigger, build.DefinitionTriggerTypeValues.PullRequest))
308+
d.Set("pull_request_trigger", flattenBuildDefinitionTriggers(buildDefinition.Triggers, yamlPrTrigger, build.DefinitionTriggerTypeValues.PullRequest))
289309
}
290310

291311
revision := 0
@@ -513,7 +533,7 @@ func hasSettingsSourceType(m *[]interface{}, t build.DefinitionTriggerType, sst
513533
hasSetting := false
514534
for _, d := range *m {
515535
if ms, ok := d.(map[string]interface{}); ok {
516-
if ms["triggerType"].(string) == string(t) {
536+
if strings.EqualFold(ms["triggerType"].(string), string(t)) {
517537
if val, ok := ms["settingsSourceType"]; ok {
518538
hasSetting = int(val.(float64)) == sst
519539
}
@@ -523,7 +543,7 @@ func hasSettingsSourceType(m *[]interface{}, t build.DefinitionTriggerType, sst
523543
return hasSetting
524544
}
525545

526-
func flattenReleaseDefinitionTriggers(m *[]interface{}, isYaml bool, t build.DefinitionTriggerType) []interface{} {
546+
func flattenBuildDefinitionTriggers(m *[]interface{}, isYaml bool, t build.DefinitionTriggerType) []interface{} {
527547
ds := make([]interface{}, 0, len(*m))
528548
for _, d := range *m {
529549
f := flattenBuildDefinitionTrigger(d, isYaml, t)
@@ -710,12 +730,12 @@ func expandBuildDefinition(d *schema.ResourceData) (*build.BuildDefinition, stri
710730
repository := repositories[0].(map[string]interface{})
711731

712732
repoName := repository["repo_name"].(string)
713-
repoType := repository["repo_type"].(string)
733+
repoType := RepoType(repository["repo_type"].(string))
714734
repoURL := ""
715-
if strings.EqualFold(repoType, "github") {
735+
if strings.EqualFold(string(repoType), string(RepoTypeValues.GitHub)) {
716736
repoURL = fmt.Sprintf("https://github.com/%s.git", repoName)
717737
}
718-
if strings.EqualFold(repoType, "bitbucket") {
738+
if strings.EqualFold(string(repoType), string(RepoTypeValues.Bitbucket)) {
719739
repoURL = fmt.Sprintf("https://bitbucket.org/%s.git", repoName)
720740
}
721741

@@ -751,7 +771,7 @@ func expandBuildDefinition(d *schema.ResourceData) (*build.BuildDefinition, stri
751771
Id: &repoName,
752772
Name: &repoName,
753773
DefaultBranch: converter.String(repository["branch_name"].(string)),
754-
Type: &repoType,
774+
Type: converter.String(string(repoType)),
755775
Properties: &map[string]string{
756776
"connectedServiceId": repository["service_connection_id"].(string),
757777
},
@@ -786,7 +806,7 @@ func validateServiceConnectionIDExistsIfNeeded(d *schema.ResourceData) error {
786806
repoType := repository["repo_type"].(string)
787807
serviceConnectionID := repository["service_connection_id"].(string)
788808

789-
if strings.EqualFold(repoType, "bitbucket") && serviceConnectionID == "" {
809+
if strings.EqualFold(repoType, string(RepoTypeValues.Bitbucket)) && serviceConnectionID == "" {
790810
return errors.New("bitbucket repositories need a referenced service connection ID")
791811
}
792812
return nil

azuredevops/resource_git_repository.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ import (
1919
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils/validate"
2020
)
2121

22+
// RepoInitType strategy for initializing the repo
23+
type RepoInitType string
24+
25+
type repoInitTypeValuesType struct {
26+
Uninitialized RepoInitType
27+
Clean RepoInitType
28+
Fork RepoInitType
29+
Import RepoInitType
30+
}
31+
32+
// RepoInitTypeValues enum of strategy for initializing the repo
33+
var RepoInitTypeValues = repoInitTypeValuesType{
34+
Uninitialized: "Uninitialized",
35+
Clean: "Clean",
36+
Fork: "Fork",
37+
Import: "Import",
38+
}
39+
2240
func resourceGitRepository() *schema.Resource {
2341
return &schema.Resource{
2442
Create: resourceGitRepositoryCreate,
@@ -84,9 +102,14 @@ func resourceGitRepository() *schema.Resource {
84102
Elem: &schema.Resource{
85103
Schema: map[string]*schema.Schema{
86104
"init_type": {
87-
Type: schema.TypeString,
88-
Required: true,
89-
ValidateFunc: validation.StringInSlice([]string{"uninitialized", "clean", "import"}, true),
105+
Type: schema.TypeString,
106+
Required: true,
107+
ValidateFunc: validation.StringInSlice([]string{
108+
string(RepoInitTypeValues.Clean),
109+
string(RepoInitTypeValues.Fork),
110+
string(RepoInitTypeValues.Import),
111+
string(RepoInitTypeValues.Uninitialized),
112+
}, false),
90113
},
91114
"source_type": {
92115
Type: schema.TypeString,

azuredevops/resource_project.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ func resourceProject() *schema.Resource {
4646
Default: "",
4747
},
4848
"visibility": {
49-
Type: schema.TypeString,
50-
Optional: true,
51-
Default: "private",
52-
ValidateFunc: validation.StringInSlice([]string{"private", "public"}, false),
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Default: core.ProjectVisibilityValues.Private,
52+
ValidateFunc: validation.StringInSlice([]string{
53+
string(core.ProjectVisibilityValues.Private),
54+
string(core.ProjectVisibilityValues.Public),
55+
}, false),
5356
},
5457
"version_control": {
55-
Type: schema.TypeString,
56-
ForceNew: true,
57-
Optional: true,
58-
Default: "Git",
59-
ValidateFunc: validation.StringInSlice([]string{"Git", "Tfvc"}, true),
58+
Type: schema.TypeString,
59+
ForceNew: true,
60+
Optional: true,
61+
Default: core.SourceControlTypesValues.Git,
62+
ValidateFunc: validation.StringInSlice([]string{
63+
string(core.SourceControlTypesValues.Git),
64+
string(core.SourceControlTypesValues.Tfvc),
65+
}, true),
6066
},
6167
"work_item_template": {
6268
Type: schema.TypeString,
@@ -246,7 +252,7 @@ func expandProject(clients *config.AggregatedClient, d *schema.ResourceData, for
246252
projectID = &parsedID
247253
}
248254

249-
visibility := d.Get("visibility").(string)
255+
visibility := core.ProjectVisibility(d.Get("visibility").(string))
250256

251257
var capabilities *map[string]map[string]string
252258
if forCreate {
@@ -264,20 +270,13 @@ func expandProject(clients *config.AggregatedClient, d *schema.ResourceData, for
264270
Id: projectID,
265271
Name: converter.String(d.Get("project_name").(string)),
266272
Description: converter.String(d.Get("description").(string)),
267-
Visibility: convertVisibility(visibility),
273+
Visibility: &visibility,
268274
Capabilities: capabilities,
269275
}
270276

271277
return project, nil
272278
}
273279

274-
func convertVisibility(v string) *core.ProjectVisibility {
275-
if strings.EqualFold(v, "public") {
276-
return &core.ProjectVisibilityValues.Public
277-
}
278-
return &core.ProjectVisibilityValues.Private
279-
}
280-
281280
func flattenProject(clients *config.AggregatedClient, d *schema.ResourceData, project *core.TeamProject) error {
282281
description := converter.ToString(project.Description, "")
283282
processTemplateID := (*project.Capabilities)["processTemplate"]["templateTypeId"]

azuredevops/resource_serviceendpoint_bitbucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func expandServiceEndpointBitBucket(d *schema.ResourceData) (*serviceendpoint.Se
2424
},
2525
Scheme: converter.String("UsernamePassword"),
2626
}
27-
serviceEndpoint.Type = converter.String("bitbucket")
27+
serviceEndpoint.Type = converter.String(string(RepoTypeValues.Bitbucket))
2828
serviceEndpoint.Url = converter.String("https://api.bitbucket.org/")
2929
return serviceEndpoint, projectID
3030
}

azuredevops/resource_user_entitlement.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/microsoft/azure-devops-go-api/azuredevops/memberentitlementmanagement"
1414
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils"
1515
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils/config"
16-
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/utils/converter"
1716
)
1817

1918
func resourceUserEntitlement() *schema.Resource {
@@ -41,11 +40,18 @@ func resourceUserEntitlement() *schema.Resource {
4140
ValidateFunc: validation.StringInSlice([]string{"aad", "ghb"}, false),
4241
},
4342
"account_license_type": {
44-
Type: schema.TypeString,
45-
Optional: true,
46-
ForceNew: true,
47-
Default: "express",
48-
ValidateFunc: validation.StringInSlice([]string{"advanced", "earlyAdopter", "express", "none", "professional", "stakeholder"}, false),
43+
Type: schema.TypeString,
44+
Optional: true,
45+
ForceNew: true,
46+
Default: licensing.AccountLicenseTypeValues.Express,
47+
ValidateFunc: validation.StringInSlice([]string{
48+
string(licensing.AccountLicenseTypeValues.None),
49+
string(licensing.AccountLicenseTypeValues.Advanced),
50+
string(licensing.AccountLicenseTypeValues.EarlyAdopter),
51+
string(licensing.AccountLicenseTypeValues.Express),
52+
string(licensing.AccountLicenseTypeValues.Professional),
53+
string(licensing.AccountLicenseTypeValues.Stakeholder),
54+
}, false),
4955
},
5056
"descriptor": {
5157
Type: schema.TypeString,
@@ -130,15 +136,12 @@ func expandUserEntitlement(d *schema.ResourceData) (*memberentitlementmanagement
130136

131137
subjectKind := "user"
132138

133-
accountLicenseType, err := converter.AccountLicenseType(d.Get("account_license_type").(string))
134-
if err != nil {
135-
return nil, err
136-
}
139+
accountLicenseType := licensing.AccountLicenseType(d.Get("account_license_type").(string))
137140

138141
return &memberentitlementmanagement.UserEntitlement{
139142

140143
AccessLevel: &licensing.AccessLevel{
141-
AccountLicenseType: accountLicenseType,
144+
AccountLicenseType: &accountLicenseType,
142145
},
143146

144147
// TODO check if it works in both case for GitHub and AzureDevOps

azuredevops/utils/converter/converter.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package converter
22

3-
import (
4-
"fmt"
5-
"strings"
6-
7-
"github.com/microsoft/azure-devops-go-api/azuredevops/licensing"
8-
)
3+
import "strings"
94

105
// String Get a pointer to a string
116
func String(value string) *string {
@@ -47,25 +42,3 @@ func ToBool(value *bool, defaultValue bool) bool {
4742

4843
return defaultValue
4944
}
50-
51-
// AccountLicenseType Get a pointer to an AccountLicenseType
52-
func AccountLicenseType(accountLicenseTypeValue string) (*licensing.AccountLicenseType, error) {
53-
var accountLicenseType licensing.AccountLicenseType
54-
switch accountLicenseTypeValue {
55-
case "none":
56-
accountLicenseType = licensing.AccountLicenseTypeValues.None
57-
case "earlyAdopter":
58-
accountLicenseType = licensing.AccountLicenseTypeValues.EarlyAdopter
59-
case "express":
60-
accountLicenseType = licensing.AccountLicenseTypeValues.Express
61-
case "professional":
62-
accountLicenseType = licensing.AccountLicenseTypeValues.Professional
63-
case "advanced":
64-
accountLicenseType = licensing.AccountLicenseTypeValues.Advanced
65-
case "stakeholder":
66-
accountLicenseType = licensing.AccountLicenseTypeValues.Stakeholder
67-
default:
68-
return nil, fmt.Errorf("Error unable to match given AccountLicenseType:%s", accountLicenseTypeValue)
69-
}
70-
return &accountLicenseType, nil
71-
}

azuredevops/utils/converter/converter_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
package converter
44

55
import (
6-
"fmt"
76
"testing"
8-
9-
"github.com/microsoft/azure-devops-go-api/azuredevops/licensing"
10-
"github.com/stretchr/testify/assert"
117
)
128

139
func TestString(t *testing.T) {
@@ -41,21 +37,3 @@ func TestBoolFalse(t *testing.T) {
4137
t.Errorf("The pointer returned references a different value")
4238
}
4339
}
44-
45-
func TestLicenseTypeAccount(t *testing.T) {
46-
assertAccountLicenseType(t, licensing.AccountLicenseTypeValues.None)
47-
assertAccountLicenseType(t, licensing.AccountLicenseTypeValues.EarlyAdopter)
48-
assertAccountLicenseType(t, licensing.AccountLicenseTypeValues.Advanced)
49-
assertAccountLicenseType(t, licensing.AccountLicenseTypeValues.Professional)
50-
assertAccountLicenseType(t, licensing.AccountLicenseTypeValues.Express)
51-
assertAccountLicenseType(t, licensing.AccountLicenseTypeValues.Professional)
52-
53-
_, err := AccountLicenseType("foo")
54-
assert.Equal(t, err.Error(), "Error unable to match given AccountLicenseType:foo")
55-
}
56-
57-
func assertAccountLicenseType(t *testing.T, accountLicenseType licensing.AccountLicenseType) {
58-
actualAccountLicenseType, err := AccountLicenseType(string(accountLicenseType))
59-
assert.Nil(t, err, fmt.Sprintf("Error should not thrown by %s", string(accountLicenseType)))
60-
assert.Equal(t, &accountLicenseType, actualAccountLicenseType, fmt.Sprintf("%s should be able to convert into the AccountLicenseType", string(accountLicenseType)))
61-
}

0 commit comments

Comments
 (0)