generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
application_data_source.go
94 lines (84 loc) · 2.69 KB
/
application_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
package application
import (
"context"
"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 DataSourceApplication() *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: dataSourceApplicationRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "Unique identifier of the application",
Type: schema.TypeString,
Optional: true,
},
"name": {
Description: "The name of the application",
Type: schema.TypeString,
Optional: true,
},
"description": {
Description: "The application description",
Type: schema.TypeString,
Optional: true,
},
"is_manual_trigger_authorized": {
Description: "When this is set to true, all manual triggers will require API Key authorization",
Type: schema.TypeBool,
Optional: true,
},
"git_sync_enabled": {
Description: "True if git sync is enabled on this application",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"git_sync_connector_id": {
Description: "The id of the git sync connector",
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// use the meta value to retrieve your client from the provider configure method
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
var app *graphql.Application
var err error
if id := d.Get("id").(string); id != "" {
// Try lookup by Id first
app, err = c.ApplicationClient.GetApplicationById(id)
if err != nil {
return diag.FromErr(err)
}
} else if name := d.Get("name").(string); name != "" {
// Fallback to lookup by name
name := d.Get("name").(string)
app, err = c.ApplicationClient.GetApplicationByName(name)
if err != nil {
return diag.FromErr(err)
}
} else {
// Throw error if neither are set
return diag.Errorf("id or name must be set")
}
d.SetId(app.Id)
d.Set("name", app.Name)
d.Set("description", app.Description)
d.Set("is_manual_trigger_authorized", app.IsManualTriggerAuthorized)
if app.GitSyncConfig != nil {
d.Set("git_sync_enabled", app.GitSyncConfig.SyncEnabled)
d.Set("git_sync_connector_id", app.GitSyncConfig.GitConnector.Id)
}
return nil
}