|
| 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 oci |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + oci_database "github.com/oracle/oci-go-sdk/v44/database" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + RegisterDatasource("oci_database_vm_cluster_update_history_entries", DatabaseVmClusterUpdateHistoryEntriesDataSource()) |
| 15 | +} |
| 16 | + |
| 17 | +func DatabaseVmClusterUpdateHistoryEntriesDataSource() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Read: readDatabaseVmClusterUpdateHistoryEntries, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "filter": dataSourceFiltersSchema(), |
| 22 | + "state": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Optional: true, |
| 25 | + }, |
| 26 | + "update_type": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + }, |
| 30 | + "vm_cluster_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "vm_cluster_update_history_entries": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Computed: true, |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + // Required |
| 40 | + |
| 41 | + // Optional |
| 42 | + |
| 43 | + // Computed |
| 44 | + "id": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "lifecycle_details": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "state": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "time_completed": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "time_started": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + "update_action": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "update_id": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "update_type": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func readDatabaseVmClusterUpdateHistoryEntries(d *schema.ResourceData, m interface{}) error { |
| 84 | + sync := &DatabaseVmClusterUpdateHistoryEntriesDataSourceCrud{} |
| 85 | + sync.D = d |
| 86 | + sync.Client = m.(*OracleClients).databaseClient() |
| 87 | + |
| 88 | + return ReadResource(sync) |
| 89 | +} |
| 90 | + |
| 91 | +type DatabaseVmClusterUpdateHistoryEntriesDataSourceCrud struct { |
| 92 | + D *schema.ResourceData |
| 93 | + Client *oci_database.DatabaseClient |
| 94 | + Res *oci_database.ListVmClusterUpdateHistoryEntriesResponse |
| 95 | +} |
| 96 | + |
| 97 | +func (s *DatabaseVmClusterUpdateHistoryEntriesDataSourceCrud) VoidState() { |
| 98 | + s.D.SetId("") |
| 99 | +} |
| 100 | + |
| 101 | +func (s *DatabaseVmClusterUpdateHistoryEntriesDataSourceCrud) Get() error { |
| 102 | + request := oci_database.ListVmClusterUpdateHistoryEntriesRequest{} |
| 103 | + |
| 104 | + if state, ok := s.D.GetOkExists("state"); ok { |
| 105 | + request.LifecycleState = oci_database.VmClusterUpdateHistoryEntrySummaryLifecycleStateEnum(state.(string)) |
| 106 | + } |
| 107 | + |
| 108 | + if updateType, ok := s.D.GetOkExists("update_type"); ok { |
| 109 | + request.UpdateType = oci_database.ListVmClusterUpdateHistoryEntriesUpdateTypeEnum(updateType.(string)) |
| 110 | + } |
| 111 | + |
| 112 | + if vmClusterId, ok := s.D.GetOkExists("vm_cluster_id"); ok { |
| 113 | + tmp := vmClusterId.(string) |
| 114 | + request.VmClusterId = &tmp |
| 115 | + } |
| 116 | + |
| 117 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "database") |
| 118 | + |
| 119 | + response, err := s.Client.ListVmClusterUpdateHistoryEntries(context.Background(), request) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + s.Res = &response |
| 125 | + request.Page = s.Res.OpcNextPage |
| 126 | + |
| 127 | + for request.Page != nil { |
| 128 | + listResponse, err := s.Client.ListVmClusterUpdateHistoryEntries(context.Background(), request) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 134 | + request.Page = listResponse.OpcNextPage |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +func (s *DatabaseVmClusterUpdateHistoryEntriesDataSourceCrud) SetData() error { |
| 141 | + if s.Res == nil { |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + s.D.SetId(GenerateDataSourceHashID("DatabaseVmClusterUpdateHistoryEntriesDataSource-", DatabaseVmClusterUpdateHistoryEntriesDataSource(), s.D)) |
| 146 | + resources := []map[string]interface{}{} |
| 147 | + |
| 148 | + for _, r := range s.Res.Items { |
| 149 | + vmClusterUpdateHistoryEntry := map[string]interface{}{} |
| 150 | + |
| 151 | + if r.Id != nil { |
| 152 | + vmClusterUpdateHistoryEntry["id"] = *r.Id |
| 153 | + } |
| 154 | + |
| 155 | + if r.LifecycleDetails != nil { |
| 156 | + vmClusterUpdateHistoryEntry["lifecycle_details"] = *r.LifecycleDetails |
| 157 | + } |
| 158 | + |
| 159 | + vmClusterUpdateHistoryEntry["state"] = r.LifecycleState |
| 160 | + |
| 161 | + if r.TimeCompleted != nil { |
| 162 | + vmClusterUpdateHistoryEntry["time_completed"] = r.TimeCompleted.String() |
| 163 | + } |
| 164 | + |
| 165 | + if r.TimeStarted != nil { |
| 166 | + vmClusterUpdateHistoryEntry["time_started"] = r.TimeStarted.String() |
| 167 | + } |
| 168 | + |
| 169 | + vmClusterUpdateHistoryEntry["update_action"] = r.UpdateAction |
| 170 | + |
| 171 | + if r.UpdateId != nil { |
| 172 | + vmClusterUpdateHistoryEntry["update_id"] = *r.UpdateId |
| 173 | + } |
| 174 | + |
| 175 | + vmClusterUpdateHistoryEntry["update_type"] = r.UpdateType |
| 176 | + |
| 177 | + resources = append(resources, vmClusterUpdateHistoryEntry) |
| 178 | + } |
| 179 | + |
| 180 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 181 | + resources = ApplyFilters(f.(*schema.Set), resources, DatabaseVmClusterUpdateHistoryEntriesDataSource().Schema["vm_cluster_update_history_entries"].Elem.(*schema.Resource).Schema) |
| 182 | + } |
| 183 | + |
| 184 | + if err := s.D.Set("vm_cluster_update_history_entries", resources); err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + return nil |
| 189 | +} |
0 commit comments