|
| 1 | +// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | + |
| 11 | + "github.com/oracle/terraform-provider-oci/crud" |
| 12 | + |
| 13 | + oci_file_storage "github.com/oracle/oci-go-sdk/filestorage" |
| 14 | +) |
| 15 | + |
| 16 | +func ExportSetResource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + State: schema.ImportStatePassthrough, |
| 20 | + }, |
| 21 | + Timeouts: crud.DefaultTimeout, |
| 22 | + Create: createExportSet, |
| 23 | + Read: readExportSet, |
| 24 | + Update: updateExportSet, |
| 25 | + Delete: deleteExportSet, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + // Required |
| 28 | + "mount_target_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + }, |
| 33 | + |
| 34 | + // Optional |
| 35 | + "display_name": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "max_fs_stat_bytes": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Optional: true, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "max_fs_stat_files": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Optional: true, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + |
| 51 | + // Computed |
| 52 | + "availability_domain": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "compartment_id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "id": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + "state": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "time_created": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "vcn_id": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func createExportSet(d *schema.ResourceData, m interface{}) error { |
| 81 | + sync := &ExportSetResourceCrud{} |
| 82 | + sync.D = d |
| 83 | + sync.Client = m.(*OracleClients).fileStorageClient |
| 84 | + |
| 85 | + return crud.CreateResource(d, sync) |
| 86 | +} |
| 87 | + |
| 88 | +func readExportSet(d *schema.ResourceData, m interface{}) error { |
| 89 | + sync := &ExportSetResourceCrud{} |
| 90 | + sync.D = d |
| 91 | + sync.Client = m.(*OracleClients).fileStorageClient |
| 92 | + |
| 93 | + return crud.ReadResource(sync) |
| 94 | +} |
| 95 | + |
| 96 | +func updateExportSet(d *schema.ResourceData, m interface{}) error { |
| 97 | + sync := &ExportSetResourceCrud{} |
| 98 | + sync.D = d |
| 99 | + sync.Client = m.(*OracleClients).fileStorageClient |
| 100 | + |
| 101 | + return crud.UpdateResource(d, sync) |
| 102 | +} |
| 103 | + |
| 104 | +func deleteExportSet(d *schema.ResourceData, m interface{}) error { |
| 105 | + // Export set is deleted when a mount target is deleted. |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +type ExportSetResourceCrud struct { |
| 110 | + crud.BaseCrud |
| 111 | + Client *oci_file_storage.FileStorageClient |
| 112 | + Res *oci_file_storage.ExportSet |
| 113 | + DisableNotFoundRetries bool |
| 114 | +} |
| 115 | + |
| 116 | +func (s *ExportSetResourceCrud) ID() string { |
| 117 | + return *s.Res.Id |
| 118 | +} |
| 119 | + |
| 120 | +func (s *ExportSetResourceCrud) Create() error { |
| 121 | + // We can't really create an ExportSet. We need to get the exportSetId from the MountTarget it is attached to. |
| 122 | + if mountTargetId, ok := s.D.GetOkExists("mount_target_id"); ok { |
| 123 | + tmp := mountTargetId.(string) |
| 124 | + |
| 125 | + request := oci_file_storage.GetMountTargetRequest{} |
| 126 | + request.MountTargetId = &tmp |
| 127 | + |
| 128 | + response, err := s.Client.GetMountTarget(context.Background(), request, getRetryOptions(s.DisableNotFoundRetries, "file_storage")...) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("getting mount target details failed with error: %s", err.Error()) |
| 131 | + } |
| 132 | + |
| 133 | + exportSetId := response.MountTarget.ExportSetId |
| 134 | + |
| 135 | + if exportSetId == nil { |
| 136 | + return fmt.Errorf("export_set_id is not available in the mount target response") |
| 137 | + } |
| 138 | + |
| 139 | + s.D.SetId(*exportSetId) |
| 140 | + return s.Update() |
| 141 | + } |
| 142 | + |
| 143 | + return fmt.Errorf("no mount_target_id value could be found") |
| 144 | +} |
| 145 | + |
| 146 | +func (s *ExportSetResourceCrud) Get() error { |
| 147 | + request := oci_file_storage.GetExportSetRequest{} |
| 148 | + |
| 149 | + tmp := s.D.Id() |
| 150 | + request.ExportSetId = &tmp |
| 151 | + |
| 152 | + response, err := s.Client.GetExportSet(context.Background(), request, getRetryOptions(s.DisableNotFoundRetries, "file_storage")...) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + s.Res = &response.ExportSet |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func (s *ExportSetResourceCrud) Update() error { |
| 162 | + request := oci_file_storage.UpdateExportSetRequest{} |
| 163 | + |
| 164 | + if displayName, ok := s.D.GetOkExists("display_name"); ok { |
| 165 | + tmp := displayName.(string) |
| 166 | + request.DisplayName = &tmp |
| 167 | + } |
| 168 | + |
| 169 | + tmp := s.D.Id() |
| 170 | + request.ExportSetId = &tmp |
| 171 | + |
| 172 | + if maxFsStatBytes, ok := s.D.GetOkExists("max_fs_stat_bytes"); ok { |
| 173 | + tmp := maxFsStatBytes.(int) |
| 174 | + request.MaxFsStatBytes = &tmp |
| 175 | + } |
| 176 | + |
| 177 | + if maxFsStatFiles, ok := s.D.GetOkExists("max_fs_stat_files"); ok { |
| 178 | + tmp := maxFsStatFiles.(int) |
| 179 | + request.MaxFsStatFiles = &tmp |
| 180 | + } |
| 181 | + |
| 182 | + response, err := s.Client.UpdateExportSet(context.Background(), request, getRetryOptions(s.DisableNotFoundRetries, "file_storage")...) |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + s.Res = &response.ExportSet |
| 188 | + return nil |
| 189 | +} |
| 190 | + |
| 191 | +func (s *ExportSetResourceCrud) Delete() error { |
| 192 | + // Export set is deleted when a mount target is deleted. |
| 193 | + return nil |
| 194 | +} |
| 195 | + |
| 196 | +func (s *ExportSetResourceCrud) SetData() { |
| 197 | + if s.Res.AvailabilityDomain != nil { |
| 198 | + s.D.Set("availability_domain", *s.Res.AvailabilityDomain) |
| 199 | + } |
| 200 | + |
| 201 | + if s.Res.CompartmentId != nil { |
| 202 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 203 | + } |
| 204 | + |
| 205 | + if s.Res.DisplayName != nil { |
| 206 | + s.D.Set("display_name", *s.Res.DisplayName) |
| 207 | + } |
| 208 | + |
| 209 | + if s.Res.Id != nil { |
| 210 | + s.D.Set("id", *s.Res.Id) |
| 211 | + } |
| 212 | + |
| 213 | + if s.Res.MaxFsStatBytes != nil { |
| 214 | + s.D.Set("max_fs_stat_bytes", *s.Res.MaxFsStatBytes) |
| 215 | + } |
| 216 | + |
| 217 | + if s.Res.MaxFsStatFiles != nil { |
| 218 | + s.D.Set("max_fs_stat_files", *s.Res.MaxFsStatFiles) |
| 219 | + } |
| 220 | + |
| 221 | + s.D.Set("state", s.Res.LifecycleState) |
| 222 | + |
| 223 | + s.D.Set("time_created", s.Res.TimeCreated.String()) |
| 224 | + |
| 225 | + if s.Res.VcnId != nil { |
| 226 | + s.D.Set("vcn_id", *s.Res.VcnId) |
| 227 | + } |
| 228 | +} |
0 commit comments