-
Notifications
You must be signed in to change notification settings - Fork 65
/
data_source_variables.go
45 lines (34 loc) · 1.23 KB
/
data_source_variables.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
package octopusdeploy
import (
"context"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceVariable() *schema.Resource {
return &schema.Resource{
Description: "Provides information about existing variables.",
ReadContext: dataSourceVariableReadByName,
Schema: getVariableDataSchema(),
}
}
func dataSourceVariableReadByName(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
spaceID := d.Get("space_id").(string)
ownerID := d.Get("owner_id").(string)
name := d.Get("name").(string)
scope := expandVariableScope(d.Get("scope"))
client := m.(*client.Client)
variables, err := variables.GetByName(client, spaceID, ownerID, name, &scope)
if err != nil {
return diag.Errorf("error reading variable with owner ID %s with name %s: %s", ownerID, name, err.Error())
}
if variables == nil {
return nil
}
if len(variables) != 1 {
return diag.Errorf("error could not find variable by name, expected to find 1 variable but got %d", len(variables))
}
setVariable(ctx, d, variables[0])
return nil
}