|
| 1 | +// Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform/helper/schema" |
| 11 | + |
| 12 | + oci_core "github.com/oracle/oci-go-sdk/core" |
| 13 | +) |
| 14 | + |
| 15 | +func RouteTableAttachmentResource() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Importer: &schema.ResourceImporter{ |
| 18 | + State: schema.ImportStatePassthrough, |
| 19 | + }, |
| 20 | + Timeouts: DefaultTimeout, |
| 21 | + Create: createRouteTableAttachment, |
| 22 | + Delete: deleteRouteTableAttachment, |
| 23 | + Read: readRouteTableAttachment, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + // Required |
| 26 | + "subnet_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + }, |
| 31 | + "route_table_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func createRouteTableAttachment(d *schema.ResourceData, m interface{}) error { |
| 41 | + sync := &RouteTableAttachmentResourceCrud{} |
| 42 | + sync.D = d |
| 43 | + sync.Client = m.(*OracleClients).virtualNetworkClient |
| 44 | + |
| 45 | + return CreateResource(d, sync) |
| 46 | +} |
| 47 | + |
| 48 | +func deleteRouteTableAttachment(d *schema.ResourceData, m interface{}) error { |
| 49 | + sync := &RouteTableAttachmentResourceCrud{} |
| 50 | + sync.D = d |
| 51 | + sync.Client = m.(*OracleClients).virtualNetworkClient |
| 52 | + sync.DisableNotFoundRetries = true |
| 53 | + |
| 54 | + return DeleteResource(d, sync) |
| 55 | +} |
| 56 | + |
| 57 | +func readRouteTableAttachment(d *schema.ResourceData, m interface{}) error { |
| 58 | + sync := &RouteTableAttachmentResourceCrud{} |
| 59 | + sync.D = d |
| 60 | + sync.Client = m.(*OracleClients).virtualNetworkClient |
| 61 | + sync.DisableNotFoundRetries = true |
| 62 | + |
| 63 | + return ReadResource(sync) |
| 64 | +} |
| 65 | + |
| 66 | +type RouteTableAttachmentResourceCrud struct { |
| 67 | + BaseCrud |
| 68 | + Client *oci_core.VirtualNetworkClient |
| 69 | + Res *oci_core.Subnet |
| 70 | + DisableNotFoundRetries bool |
| 71 | +} |
| 72 | + |
| 73 | +func getRouteTableAttachmentId(subnetId string, routeTableId string) string { |
| 74 | + return subnetId + "/" + routeTableId |
| 75 | +} |
| 76 | + |
| 77 | +func parseRouteTableAttachmentId(id string) (subnetId string, routTableId string, err error) { |
| 78 | + parts := strings.Split(id, "/") |
| 79 | + if len(parts) < 2 { |
| 80 | + err = fmt.Errorf("illegal id %s encountered", id) |
| 81 | + } |
| 82 | + subnetId, routTableId = parts[0], parts[1] |
| 83 | + |
| 84 | + return |
| 85 | +} |
| 86 | + |
| 87 | +func (s *RouteTableAttachmentResourceCrud) ID() string { |
| 88 | + return getRouteTableAttachmentId(*s.Res.Id, *s.Res.RouteTableId) |
| 89 | +} |
| 90 | + |
| 91 | +func (s *RouteTableAttachmentResourceCrud) Create() error { |
| 92 | + request := oci_core.UpdateSubnetRequest{} |
| 93 | + |
| 94 | + if routeTableId, ok := s.D.GetOkExists("route_table_id"); ok { |
| 95 | + tmp := routeTableId.(string) |
| 96 | + request.RouteTableId = &tmp |
| 97 | + } |
| 98 | + |
| 99 | + if subnetId, ok := s.D.GetOkExists("subnet_id"); ok { |
| 100 | + tmp := subnetId.(string) |
| 101 | + request.SubnetId = &tmp |
| 102 | + } |
| 103 | + |
| 104 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "core") |
| 105 | + response, err := s.Client.UpdateSubnet(context.Background(), request) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + s.Res = &response.Subnet |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (s *RouteTableAttachmentResourceCrud) Get() error { |
| 115 | + |
| 116 | + subnetId, routeTableId, err := parseRouteTableAttachmentId(s.D.Id()) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + request := oci_core.GetSubnetRequest{} |
| 121 | + request.SubnetId = &subnetId |
| 122 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "core") |
| 123 | + response, err := s.Client.GetSubnet(context.Background(), request) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if response.Subnet.RouteTableId == nil || *response.Subnet.RouteTableId != routeTableId { |
| 129 | + return fmt.Errorf("inconsistent route table id received: %s expected: %s", *response.Subnet.RouteTableId, routeTableId) |
| 130 | + } |
| 131 | + |
| 132 | + s.Res = &response.Subnet |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func (s *RouteTableAttachmentResourceCrud) Delete() error { |
| 137 | + |
| 138 | + var subnetIdStr = s.D.Get("subnet_id").(string) |
| 139 | + var retryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "core") |
| 140 | + |
| 141 | + subnetRequest := oci_core.GetSubnetRequest{} |
| 142 | + subnetRequest.SubnetId = &subnetIdStr |
| 143 | + subnetRequest.RequestMetadata.RetryPolicy = retryPolicy |
| 144 | + subnetResponse, err := s.Client.GetSubnet(context.Background(), subnetRequest) |
| 145 | + if err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + vcnRequest := oci_core.GetVcnRequest{} |
| 150 | + vcnRequest.VcnId = subnetResponse.VcnId |
| 151 | + vcnRequest.RequestMetadata.RetryPolicy = retryPolicy |
| 152 | + vcnResponse, err := s.Client.GetVcn(context.Background(), vcnRequest) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + updateSubnetRequest := oci_core.UpdateSubnetRequest{} |
| 158 | + updateSubnetRequest.RouteTableId = vcnResponse.DefaultRouteTableId |
| 159 | + updateSubnetRequest.SubnetId = &subnetIdStr |
| 160 | + updateSubnetRequest.RequestMetadata.RetryPolicy = retryPolicy |
| 161 | + updateSubnetResponse, err := s.Client.UpdateSubnet(context.Background(), updateSubnetRequest) |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + |
| 166 | + s.Res = &updateSubnetResponse.Subnet |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func (s *RouteTableAttachmentResourceCrud) SetData() error { |
| 171 | + |
| 172 | + s.D.Set("subnet_id", *s.Res.Id) |
| 173 | + |
| 174 | + if s.Res.RouteTableId != nil { |
| 175 | + s.D.Set("route_table_id", *s.Res.RouteTableId) |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
0 commit comments