generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
git_connector_data_source.go
155 lines (144 loc) · 4.52 KB
/
git_connector_data_source.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
package connector
import (
"context"
"fmt"
"github.com/harness/harness-go-sdk/harness/cd/graphql"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceGitConnector() *schema.Resource {
return &schema.Resource{
// This description is used by the documentation generator and the language server.
Description: "Data source for retrieving a Harness application",
ReadContext: dataSourceGitConnectorRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "Id of the git connector.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
ConflictsWith: []string{"name"},
},
"name": {
Description: "The name of the git connector.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
ConflictsWith: []string{"id"},
},
"created_at": {
Description: "The time the git connector was created.",
Type: schema.TypeString,
Computed: true,
},
"url": {
Description: "The URL of the git repository or account/organization.",
Type: schema.TypeString,
Computed: true,
},
"branch": {
Description: "The branch of the git connector to use.",
Type: schema.TypeString,
Computed: true,
},
"commit_details": {
Description: "Custom details to use when making commits using this git connector.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"author_email_id": {
Description: "The email id of the author.",
Type: schema.TypeString,
Computed: true,
},
"author_name": {
Description: "The name of the author.",
Type: schema.TypeString,
Computed: true,
},
"message": {
Description: "Commit message.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"delegate_selectors": {
Description: "Delegate selectors to apply to this git connector.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"generate_webhook_url": {
Description: "Boolean indicating whether or not to generate a webhook url.",
Type: schema.TypeBool,
Computed: true,
},
"webhook_url": {
Description: "The generated webhook url.",
Type: schema.TypeString,
Computed: true,
},
"password_secret_id": {
Description: "The id of the secret for connecting to the git repository.",
Type: schema.TypeString,
Computed: true,
},
"ssh_setting_id": {
Description: "The id of the SSH secret to use.",
Type: schema.TypeString,
Computed: true,
},
"url_type": {
Description: fmt.Sprintf("The type of git url being used. Options are `%s`, and `%s`.", graphql.GitUrlTypes.Account, graphql.GitUrlTypes.Repo),
Type: schema.TypeString,
Computed: true,
},
"username": {
Description: "The name of the user used to connect to the git repository.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceGitConnectorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
var conn *graphql.GitConnector
var err error
if id, ok := d.GetOk("id"); ok {
conn, err = c.ConnectorClient.GetGitConnectorById(id.(string))
} else if name, ok := d.GetOk("name"); ok {
conn, err = c.ConnectorClient.GetGitConnectorByName(name.(string))
} else {
return diag.Errorf("Must specify either `id` or `name`.")
}
if err != nil {
return diag.FromErr(err)
}
if conn == nil {
return diag.Errorf("Git connector not found.")
}
d.SetId(conn.Id)
d.Set("name", conn.Name)
d.Set("created_at", conn.CreatedAt.String())
d.Set("url", conn.Url)
d.Set("branch", conn.Branch)
d.Set("password_secret_id", conn.PasswordSecretId)
d.Set("ssh_setting_id", conn.SSHSettingId)
d.Set("webhook_url", conn.WebhookUrl)
d.Set("url_type", conn.UrlType)
d.Set("username", conn.UserName)
d.Set("commit_details", flattenCommitDetails(conn.CustomCommitDetails))
d.Set("delegate_selectors", utils.FlattenDelgateSelectors(conn.DelegateSelectors))
return nil
}