-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_system_information.go
80 lines (72 loc) · 2 KB
/
data_source_system_information.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
package junos
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceSystemInformation() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSystemInformationRead,
Schema: map[string]*schema.Schema{
"hardware_model": {
Type: schema.TypeString,
Computed: true,
},
"os_name": {
Type: schema.TypeString,
Computed: true,
},
"os_version": {
Type: schema.TypeString,
Computed: true,
},
"serial_number": {
Type: schema.TypeString,
Computed: true,
},
"cluster_node": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceSystemInformationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
j, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(j)
// Catches case where hostname is not set
if j.SystemInformation.HostName != "" {
d.SetId(j.SystemInformation.HostName)
} else {
d.SetId("Null-Hostname")
}
if tfErr := d.Set("hardware_model", j.SystemInformation.HardwareModel); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("os_name", j.SystemInformation.OsName); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("os_version", j.SystemInformation.OsVersion); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("serial_number", j.SystemInformation.SerialNumber); tfErr != nil {
panic(tfErr)
}
// Fix recommended in https://stackoverflow.com/a/23725010 due ot the lack of being able to Unmarshal self-closing
// xml tags. This issue is tracked here - https://github.com/golang/go/issues/21399
// Pointer to bool in sysInfo struct will be nil if the tag does not exist
if j.SystemInformation.ClusterNode != nil {
if tfErr := d.Set("cluster_node", true); tfErr != nil {
panic(tfErr)
}
} else {
if tfErr := d.Set("cluster_node", false); tfErr != nil {
panic(tfErr)
}
}
return nil
}