|
| 1 | +// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package oci |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net/url" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform/helper/schema" |
| 14 | + |
| 15 | + oci_database "github.com/oracle/oci-go-sdk/database" |
| 16 | +) |
| 17 | + |
| 18 | +func DatabaseDbNodeConsoleConnectionResource() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Timeouts: DefaultTimeout, |
| 24 | + Create: createDatabaseDbNodeConsoleConnection, |
| 25 | + Read: readDatabaseDbNodeConsoleConnection, |
| 26 | + Delete: deleteDatabaseDbNodeConsoleConnection, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + // Required |
| 29 | + "db_node_id": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + }, |
| 34 | + "public_key": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + |
| 40 | + // Optional |
| 41 | + |
| 42 | + // Computed |
| 43 | + "compartment_id": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "connection_string": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "fingerprint": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "state": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func createDatabaseDbNodeConsoleConnection(d *schema.ResourceData, m interface{}) error { |
| 64 | + sync := &DatabaseDbNodeConsoleConnectionResourceCrud{} |
| 65 | + sync.D = d |
| 66 | + sync.Client = m.(*OracleClients).databaseClient |
| 67 | + |
| 68 | + return CreateResource(d, sync) |
| 69 | +} |
| 70 | + |
| 71 | +func readDatabaseDbNodeConsoleConnection(d *schema.ResourceData, m interface{}) error { |
| 72 | + sync := &DatabaseDbNodeConsoleConnectionResourceCrud{} |
| 73 | + sync.D = d |
| 74 | + sync.Client = m.(*OracleClients).databaseClient |
| 75 | + |
| 76 | + return ReadResource(sync) |
| 77 | +} |
| 78 | + |
| 79 | +func deleteDatabaseDbNodeConsoleConnection(d *schema.ResourceData, m interface{}) error { |
| 80 | + sync := &DatabaseDbNodeConsoleConnectionResourceCrud{} |
| 81 | + sync.D = d |
| 82 | + sync.Client = m.(*OracleClients).databaseClient |
| 83 | + sync.DisableNotFoundRetries = true |
| 84 | + |
| 85 | + return DeleteResource(d, sync) |
| 86 | +} |
| 87 | + |
| 88 | +type DatabaseDbNodeConsoleConnectionResourceCrud struct { |
| 89 | + BaseCrud |
| 90 | + Client *oci_database.DatabaseClient |
| 91 | + Res *oci_database.ConsoleConnection |
| 92 | + DisableNotFoundRetries bool |
| 93 | +} |
| 94 | + |
| 95 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) ID() string { |
| 96 | + return getDbNodeConsoleConnectionCompositeId(s.D.Get("db_node_id").(string), *s.Res.Id) |
| 97 | +} |
| 98 | + |
| 99 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) CreatedPending() []string { |
| 100 | + return []string{ |
| 101 | + string(oci_database.ConsoleConnectionLifecycleStateCreating), |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) CreatedTarget() []string { |
| 106 | + return []string{ |
| 107 | + string(oci_database.ConsoleConnectionLifecycleStateActive), |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) DeletedPending() []string { |
| 112 | + return []string{ |
| 113 | + string(oci_database.ConsoleConnectionLifecycleStateDeleting), |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) DeletedTarget() []string { |
| 118 | + return []string{ |
| 119 | + string(oci_database.ConsoleConnectionLifecycleStateDeleted), |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) Create() error { |
| 124 | + request := oci_database.CreateConsoleConnectionRequest{} |
| 125 | + |
| 126 | + if dbNodeId, ok := s.D.GetOkExists("db_node_id"); ok { |
| 127 | + tmp := dbNodeId.(string) |
| 128 | + request.DbNodeId = &tmp |
| 129 | + } |
| 130 | + |
| 131 | + if publicKey, ok := s.D.GetOkExists("public_key"); ok { |
| 132 | + tmp := publicKey.(string) |
| 133 | + request.PublicKey = &tmp |
| 134 | + } |
| 135 | + |
| 136 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "database") |
| 137 | + |
| 138 | + response, err := s.Client.CreateConsoleConnection(context.Background(), request) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + s.Res = &response.ConsoleConnection |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) Get() error { |
| 148 | + request := oci_database.GetConsoleConnectionRequest{} |
| 149 | + |
| 150 | + if dbNodeId, ok := s.D.GetOkExists("db_node_id"); ok { |
| 151 | + tmp := dbNodeId.(string) |
| 152 | + request.DbNodeId = &tmp |
| 153 | + } |
| 154 | + |
| 155 | + tmp := s.D.Id() |
| 156 | + request.ConsoleConnectionId = &tmp |
| 157 | + |
| 158 | + dbNodeId, id, err := parseDbNodeConsoleConnectionCompositeId(s.D.Id()) |
| 159 | + if err == nil { |
| 160 | + request.DbNodeId = &dbNodeId |
| 161 | + request.ConsoleConnectionId = &id |
| 162 | + } else { |
| 163 | + log.Printf("[WARN] Get() unable to parse current ID: %s", s.D.Id()) |
| 164 | + } |
| 165 | + |
| 166 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "database") |
| 167 | + |
| 168 | + response, err := s.Client.GetConsoleConnection(context.Background(), request) |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + |
| 173 | + s.Res = &response.ConsoleConnection |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) Delete() error { |
| 178 | + request := oci_database.DeleteConsoleConnectionRequest{} |
| 179 | + |
| 180 | + if dbNodeId, ok := s.D.GetOkExists("db_node_id"); ok { |
| 181 | + tmp := dbNodeId.(string) |
| 182 | + request.DbNodeId = &tmp |
| 183 | + } |
| 184 | + |
| 185 | + dbNodeId, id, error := parseDbNodeConsoleConnectionCompositeId(s.D.Id()) |
| 186 | + if error == nil { |
| 187 | + request.DbNodeId = &dbNodeId |
| 188 | + request.ConsoleConnectionId = &id |
| 189 | + } else { |
| 190 | + log.Printf("[WARN] Delete() unable to parse current ID: %s", s.D.Id()) |
| 191 | + } |
| 192 | + |
| 193 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "database") |
| 194 | + |
| 195 | + _, err := s.Client.DeleteConsoleConnection(context.Background(), request) |
| 196 | + return err |
| 197 | +} |
| 198 | + |
| 199 | +func (s *DatabaseDbNodeConsoleConnectionResourceCrud) SetData() error { |
| 200 | + |
| 201 | + dbNodeId, id, err := parseDbNodeConsoleConnectionCompositeId(s.D.Id()) |
| 202 | + if err == nil { |
| 203 | + s.D.Set("db_node_id", &dbNodeId) |
| 204 | + s.D.Set("id", &id) |
| 205 | + } else { |
| 206 | + log.Printf("[WARN] SetData() unable to parse current ID: %s", s.D.Id()) |
| 207 | + } |
| 208 | + |
| 209 | + if s.Res.CompartmentId != nil { |
| 210 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 211 | + } |
| 212 | + |
| 213 | + if s.Res.ConnectionString != nil { |
| 214 | + s.D.Set("connection_string", *s.Res.ConnectionString) |
| 215 | + } |
| 216 | + |
| 217 | + if s.Res.DbNodeId != nil { |
| 218 | + s.D.Set("db_node_id", *s.Res.DbNodeId) |
| 219 | + } |
| 220 | + |
| 221 | + if s.Res.Fingerprint != nil { |
| 222 | + s.D.Set("fingerprint", *s.Res.Fingerprint) |
| 223 | + } |
| 224 | + |
| 225 | + s.D.Set("state", s.Res.LifecycleState) |
| 226 | + |
| 227 | + return nil |
| 228 | +} |
| 229 | + |
| 230 | +func getDbNodeConsoleConnectionCompositeId(dbNodeId string, id string) string { |
| 231 | + dbNodeId = url.PathEscape(dbNodeId) |
| 232 | + id = url.PathEscape(id) |
| 233 | + compositeId := "dbNodes/" + dbNodeId + "/consoleConnections/" + id |
| 234 | + return compositeId |
| 235 | +} |
| 236 | + |
| 237 | +func parseDbNodeConsoleConnectionCompositeId(compositeId string) (dbNodeId string, id string, err error) { |
| 238 | + parts := strings.Split(compositeId, "/") |
| 239 | + match, _ := regexp.MatchString("dbNodes/.*/consoleConnections/.*", compositeId) |
| 240 | + if !match || len(parts) != 4 { |
| 241 | + err = fmt.Errorf("illegal compositeId %s encountered", compositeId) |
| 242 | + return |
| 243 | + } |
| 244 | + dbNodeId, _ = url.PathUnescape(parts[1]) |
| 245 | + id, _ = url.PathUnescape(parts[3]) |
| 246 | + |
| 247 | + return |
| 248 | +} |
0 commit comments