|
| 1 | +// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package oci |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "log" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | + oci_database "github.com/oracle/oci-go-sdk/database" |
| 11 | +) |
| 12 | + |
| 13 | +func DatabaseDbNodeConsoleConnectionDataSource() *schema.Resource { |
| 14 | + fieldMap := make(map[string]*schema.Schema) |
| 15 | + fieldMap["db_node_id"] = &schema.Schema{ |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + } |
| 19 | + fieldMap["id"] = &schema.Schema{ |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + } |
| 23 | + return GetSingularDataSourceItemSchema(DatabaseDbNodeConsoleConnectionResource(), fieldMap, readSingularDatabaseDbNodeConsoleConnection) |
| 24 | +} |
| 25 | + |
| 26 | +func readSingularDatabaseDbNodeConsoleConnection(d *schema.ResourceData, m interface{}) error { |
| 27 | + sync := &DatabaseDbNodeConsoleConnectionDataSourceCrud{} |
| 28 | + sync.D = d |
| 29 | + sync.Client = m.(*OracleClients).databaseClient |
| 30 | + |
| 31 | + return ReadResource(sync) |
| 32 | +} |
| 33 | + |
| 34 | +type DatabaseDbNodeConsoleConnectionDataSourceCrud struct { |
| 35 | + D *schema.ResourceData |
| 36 | + Client *oci_database.DatabaseClient |
| 37 | + Res *oci_database.GetConsoleConnectionResponse |
| 38 | +} |
| 39 | + |
| 40 | +func (s *DatabaseDbNodeConsoleConnectionDataSourceCrud) VoidState() { |
| 41 | + s.D.SetId("") |
| 42 | +} |
| 43 | + |
| 44 | +func (s *DatabaseDbNodeConsoleConnectionDataSourceCrud) Get() error { |
| 45 | + request := oci_database.GetConsoleConnectionRequest{} |
| 46 | + |
| 47 | + if dbNodeId, ok := s.D.GetOkExists("db_node_id"); ok { |
| 48 | + tmp := dbNodeId.(string) |
| 49 | + request.DbNodeId = &tmp |
| 50 | + } |
| 51 | + |
| 52 | + if id, ok := s.D.GetOkExists("id"); ok { |
| 53 | + tmp := id.(string) |
| 54 | + request.ConsoleConnectionId = &tmp |
| 55 | + } |
| 56 | + |
| 57 | + dbNodeId, id, error := parseDbNodeConsoleConnectionCompositeId(s.D.Id()) |
| 58 | + if error == nil { |
| 59 | + request.DbNodeId = &dbNodeId |
| 60 | + request.ConsoleConnectionId = &id |
| 61 | + } else { |
| 62 | + log.Printf("[WARN] Get() unable to parse current ID: %s", s.D.Id()) |
| 63 | + } |
| 64 | + |
| 65 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "database") |
| 66 | + |
| 67 | + response, err := s.Client.GetConsoleConnection(context.Background(), request) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + s.Res = &response |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (s *DatabaseDbNodeConsoleConnectionDataSourceCrud) SetData() error { |
| 77 | + if s.Res == nil { |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + s.D.SetId(*s.Res.Id) |
| 82 | + |
| 83 | + if s.Res.CompartmentId != nil { |
| 84 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 85 | + } |
| 86 | + |
| 87 | + if s.Res.ConnectionString != nil { |
| 88 | + s.D.Set("connection_string", *s.Res.ConnectionString) |
| 89 | + } |
| 90 | + |
| 91 | + if s.Res.Fingerprint != nil { |
| 92 | + s.D.Set("fingerprint", *s.Res.Fingerprint) |
| 93 | + } |
| 94 | + |
| 95 | + s.D.Set("state", s.Res.LifecycleState) |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments