-
Notifications
You must be signed in to change notification settings - Fork 3
/
datasource_machine.go
86 lines (70 loc) · 2.08 KB
/
datasource_machine.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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
"log"
"octopus"
)
const (
datasourceKeyMachineSlug = "slug"
datasourceKeyMachineName = "name"
datasourceKeyMachineURI = "uri"
datasourceKeyMachineThumbprint = "thumbprint"
)
func datasourceMachine() *schema.Resource {
return &schema.Resource{
Read: datasourceMachineRead,
Exists: datasourceMachineExists,
Schema: map[string]*schema.Schema{
datasourceKeyMachineSlug: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The machine slug (last segment of the machine URL in Octopus UI).",
},
datasourceKeyMachineName: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The machine name.",
},
datasourceKeyMachineURI: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The machine URI.",
},
datasourceKeyMachineThumbprint: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The machine thumbprint.",
},
},
}
}
// Read a machine data-source.
func datasourceMachineRead(data *schema.ResourceData, provider interface{}) error {
slug := data.Get(datasourceKeyMachineSlug).(string)
log.Printf("Read machine '%s'.", slug)
client := provider.(*octopus.Client)
machine, err := client.GetMachine(slug)
if err != nil {
return err
}
if machine == nil {
// Machine has been deleted.
data.SetId("")
return nil
}
data.SetId(machine.ID)
data.Set(datasourceKeyMachineName, machine.Name)
data.Set(datasourceKeyMachineURI, machine.URI)
data.Set(datasourceKeyMachineThumbprint, machine.Thumbprint)
return nil
}
// Determine whether a machine datasource exists.
func datasourceMachineExists(data *schema.ResourceData, provider interface{}) (exists bool, err error) {
slug := data.Get(datasourceKeyMachineSlug).(string)
log.Printf("Check if machine '%s' exists.", slug)
client := provider.(*octopus.Client)
var machine *octopus.Machine
machine, err = client.GetMachine(slug)
exists = machine != nil
return
}