/
core_instance_credential_data_source.go
86 lines (68 loc) · 1.85 KB
/
core_instance_credential_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
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"context"
"github.com/hashicorp/terraform/helper/schema"
oci_core "github.com/oracle/oci-go-sdk/core"
)
func CoreInstanceCredentialDataSource() *schema.Resource {
return &schema.Resource{
Read: readSingularCoreInstanceCredential,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
},
// Computed
"password": {
Type: schema.TypeString,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func readSingularCoreInstanceCredential(d *schema.ResourceData, m interface{}) error {
sync := &CoreInstanceCredentialDataSourceCrud{}
sync.D = d
sync.Client = m.(*OracleClients).computeClient
return ReadResource(sync)
}
type CoreInstanceCredentialDataSourceCrud struct {
D *schema.ResourceData
Client *oci_core.ComputeClient
Res *oci_core.GetWindowsInstanceInitialCredentialsResponse
}
func (s *CoreInstanceCredentialDataSourceCrud) VoidState() {
s.D.SetId("")
}
func (s *CoreInstanceCredentialDataSourceCrud) Get() error {
request := oci_core.GetWindowsInstanceInitialCredentialsRequest{}
if instanceId, ok := s.D.GetOkExists("instance_id"); ok {
tmp := instanceId.(string)
request.InstanceId = &tmp
}
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core")
response, err := s.Client.GetWindowsInstanceInitialCredentials(context.Background(), request)
if err != nil {
return err
}
s.Res = &response
return nil
}
func (s *CoreInstanceCredentialDataSourceCrud) SetData() error {
if s.Res == nil {
return nil
}
s.D.SetId(GenerateDataSourceID())
if s.Res.Password != nil {
s.D.Set("password", *s.Res.Password)
}
if s.Res.Username != nil {
s.D.Set("username", *s.Res.Username)
}
return nil
}