|
| 1 | +// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package datascience |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + oci_datascience "github.com/oracle/oci-go-sdk/v56/datascience" |
| 11 | + |
| 12 | + "github.com/terraform-providers/terraform-provider-oci/internal/client" |
| 13 | + "github.com/terraform-providers/terraform-provider-oci/internal/tfresource" |
| 14 | +) |
| 15 | + |
| 16 | +func DatascienceFastLaunchJobConfigsDataSource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: readDatascienceFastLaunchJobConfigs, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "filter": tfresource.DataSourceFiltersSchema(), |
| 21 | + "compartment_id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "fast_launch_job_configs": { |
| 26 | + Type: schema.TypeList, |
| 27 | + Computed: true, |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + // Required |
| 31 | + |
| 32 | + // Optional |
| 33 | + |
| 34 | + // Computed |
| 35 | + "core_count": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "managed_egress_support": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "memory_in_gbs": { |
| 44 | + Type: schema.TypeInt, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "shape_name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "shape_series": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func readDatascienceFastLaunchJobConfigs(d *schema.ResourceData, m interface{}) error { |
| 67 | + sync := &DatascienceFastLaunchJobConfigsDataSourceCrud{} |
| 68 | + sync.D = d |
| 69 | + sync.Client = m.(*client.OracleClients).DataScienceClient() |
| 70 | + |
| 71 | + return tfresource.ReadResource(sync) |
| 72 | +} |
| 73 | + |
| 74 | +type DatascienceFastLaunchJobConfigsDataSourceCrud struct { |
| 75 | + D *schema.ResourceData |
| 76 | + Client *oci_datascience.DataScienceClient |
| 77 | + Res *oci_datascience.ListFastLaunchJobConfigsResponse |
| 78 | +} |
| 79 | + |
| 80 | +func (s *DatascienceFastLaunchJobConfigsDataSourceCrud) VoidState() { |
| 81 | + s.D.SetId("") |
| 82 | +} |
| 83 | + |
| 84 | +func (s *DatascienceFastLaunchJobConfigsDataSourceCrud) Get() error { |
| 85 | + request := oci_datascience.ListFastLaunchJobConfigsRequest{} |
| 86 | + |
| 87 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 88 | + tmp := compartmentId.(string) |
| 89 | + request.CompartmentId = &tmp |
| 90 | + } |
| 91 | + |
| 92 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(false, "datascience") |
| 93 | + |
| 94 | + response, err := s.Client.ListFastLaunchJobConfigs(context.Background(), request) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + s.Res = &response |
| 100 | + request.Page = s.Res.OpcNextPage |
| 101 | + |
| 102 | + for request.Page != nil { |
| 103 | + listResponse, err := s.Client.ListFastLaunchJobConfigs(context.Background(), request) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 109 | + request.Page = listResponse.OpcNextPage |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (s *DatascienceFastLaunchJobConfigsDataSourceCrud) SetData() error { |
| 116 | + if s.Res == nil { |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + s.D.SetId(tfresource.GenerateDataSourceHashID("DatascienceFastLaunchJobConfigsDataSource-", DatascienceFastLaunchJobConfigsDataSource(), s.D)) |
| 121 | + resources := []map[string]interface{}{} |
| 122 | + |
| 123 | + for _, r := range s.Res.Items { |
| 124 | + fastLaunchJobConfig := map[string]interface{}{} |
| 125 | + |
| 126 | + if r.CoreCount != nil { |
| 127 | + fastLaunchJobConfig["core_count"] = *r.CoreCount |
| 128 | + } |
| 129 | + |
| 130 | + fastLaunchJobConfig["managed_egress_support"] = r.ManagedEgressSupport |
| 131 | + |
| 132 | + if r.MemoryInGBs != nil { |
| 133 | + fastLaunchJobConfig["memory_in_gbs"] = *r.MemoryInGBs |
| 134 | + } |
| 135 | + |
| 136 | + if r.Name != nil { |
| 137 | + fastLaunchJobConfig["name"] = *r.Name |
| 138 | + } |
| 139 | + |
| 140 | + if r.ShapeName != nil { |
| 141 | + fastLaunchJobConfig["shape_name"] = *r.ShapeName |
| 142 | + } |
| 143 | + |
| 144 | + fastLaunchJobConfig["shape_series"] = r.ShapeSeries |
| 145 | + |
| 146 | + resources = append(resources, fastLaunchJobConfig) |
| 147 | + } |
| 148 | + |
| 149 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 150 | + resources = tfresource.ApplyFilters(f.(*schema.Set), resources, DatascienceFastLaunchJobConfigsDataSource().Schema["fast_launch_job_configs"].Elem.(*schema.Resource).Schema) |
| 151 | + } |
| 152 | + |
| 153 | + if err := s.D.Set("fast_launch_job_configs", resources); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments