|
| 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 | + "bytes" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "io/ioutil" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 13 | + oci_log_analytics "github.com/oracle/oci-go-sdk/v47/loganalytics" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + RegisterResource("oci_log_analytics_log_analytics_import_custom_content", LogAnalyticsLogAnalyticsImportCustomContentResource()) |
| 18 | +} |
| 19 | + |
| 20 | +func LogAnalyticsLogAnalyticsImportCustomContentResource() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: schema.ImportStatePassthrough, |
| 24 | + }, |
| 25 | + Timeouts: DefaultTimeout, |
| 26 | + Create: createLogAnalyticsLogAnalyticsImportCustomContent, |
| 27 | + Read: readLogAnalyticsLogAnalyticsImportCustomContent, |
| 28 | + Delete: deleteLogAnalyticsLogAnalyticsImportCustomContent, |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + // Required |
| 31 | + "import_custom_content_file": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + }, |
| 36 | + "namespace": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + ForceNew: true, |
| 40 | + }, |
| 41 | + |
| 42 | + // Optional |
| 43 | + "is_overwrite": { |
| 44 | + Type: schema.TypeBool, |
| 45 | + Optional: true, |
| 46 | + Computed: true, |
| 47 | + ForceNew: true, |
| 48 | + }, |
| 49 | + |
| 50 | + // Computed |
| 51 | + "change_list": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Computed: true, |
| 54 | + MaxItems: 1, |
| 55 | + MinItems: 1, |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + // Required |
| 59 | + |
| 60 | + // Optional |
| 61 | + |
| 62 | + // Computed |
| 63 | + "conflict_field_display_names": { |
| 64 | + Type: schema.TypeList, |
| 65 | + Computed: true, |
| 66 | + Elem: &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + }, |
| 69 | + }, |
| 70 | + "conflict_parser_names": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Computed: true, |
| 73 | + Elem: &schema.Schema{ |
| 74 | + Type: schema.TypeString, |
| 75 | + }, |
| 76 | + }, |
| 77 | + "conflict_source_names": { |
| 78 | + Type: schema.TypeList, |
| 79 | + Computed: true, |
| 80 | + Elem: &schema.Schema{ |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + }, |
| 84 | + "created_field_display_names": { |
| 85 | + Type: schema.TypeList, |
| 86 | + Computed: true, |
| 87 | + Elem: &schema.Schema{ |
| 88 | + Type: schema.TypeString, |
| 89 | + }, |
| 90 | + }, |
| 91 | + "created_parser_names": { |
| 92 | + Type: schema.TypeList, |
| 93 | + Computed: true, |
| 94 | + Elem: &schema.Schema{ |
| 95 | + Type: schema.TypeString, |
| 96 | + }, |
| 97 | + }, |
| 98 | + "created_source_names": { |
| 99 | + Type: schema.TypeList, |
| 100 | + Computed: true, |
| 101 | + Elem: &schema.Schema{ |
| 102 | + Type: schema.TypeString, |
| 103 | + }, |
| 104 | + }, |
| 105 | + "updated_field_display_names": { |
| 106 | + Type: schema.TypeList, |
| 107 | + Computed: true, |
| 108 | + Elem: &schema.Schema{ |
| 109 | + Type: schema.TypeString, |
| 110 | + }, |
| 111 | + }, |
| 112 | + "updated_parser_names": { |
| 113 | + Type: schema.TypeList, |
| 114 | + Computed: true, |
| 115 | + Elem: &schema.Schema{ |
| 116 | + Type: schema.TypeString, |
| 117 | + }, |
| 118 | + }, |
| 119 | + "updated_source_names": { |
| 120 | + Type: schema.TypeList, |
| 121 | + Computed: true, |
| 122 | + Elem: &schema.Schema{ |
| 123 | + Type: schema.TypeString, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + "content_name": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + }, |
| 133 | + "field_names": { |
| 134 | + Type: schema.TypeList, |
| 135 | + Computed: true, |
| 136 | + Elem: &schema.Schema{ |
| 137 | + Type: schema.TypeString, |
| 138 | + }, |
| 139 | + }, |
| 140 | + "parser_names": { |
| 141 | + Type: schema.TypeList, |
| 142 | + Computed: true, |
| 143 | + Elem: &schema.Schema{ |
| 144 | + Type: schema.TypeString, |
| 145 | + }, |
| 146 | + }, |
| 147 | + "source_names": { |
| 148 | + Type: schema.TypeList, |
| 149 | + Computed: true, |
| 150 | + Elem: &schema.Schema{ |
| 151 | + Type: schema.TypeString, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func createLogAnalyticsLogAnalyticsImportCustomContent(d *schema.ResourceData, m interface{}) error { |
| 159 | + sync := &LogAnalyticsLogAnalyticsImportCustomContentResourceCrud{} |
| 160 | + sync.D = d |
| 161 | + sync.Client = m.(*OracleClients).logAnalyticsClient() |
| 162 | + |
| 163 | + return CreateResource(d, sync) |
| 164 | +} |
| 165 | + |
| 166 | +func readLogAnalyticsLogAnalyticsImportCustomContent(d *schema.ResourceData, m interface{}) error { |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func deleteLogAnalyticsLogAnalyticsImportCustomContent(d *schema.ResourceData, m interface{}) error { |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +type LogAnalyticsLogAnalyticsImportCustomContentResourceCrud struct { |
| 175 | + BaseCrud |
| 176 | + Client *oci_log_analytics.LogAnalyticsClient |
| 177 | + Res *oci_log_analytics.LogAnalyticsImportCustomContent |
| 178 | + DisableNotFoundRetries bool |
| 179 | +} |
| 180 | + |
| 181 | +func (s *LogAnalyticsLogAnalyticsImportCustomContentResourceCrud) ID() string { |
| 182 | + return GenerateDataSourceHashID("LogAnalyticsLogAnalyticsImportCustomContentResource-", LogAnalyticsLogAnalyticsImportCustomContentResource(), s.D) |
| 183 | +} |
| 184 | + |
| 185 | +func (s *LogAnalyticsLogAnalyticsImportCustomContentResourceCrud) Create() error { |
| 186 | + request := oci_log_analytics.ImportCustomContentRequest{} |
| 187 | + |
| 188 | + if importFile, ok := s.D.GetOkExists("import_custom_content_file"); ok { |
| 189 | + tmp := importFile.(string) |
| 190 | + contents, err := ioutil.ReadFile(tmp) |
| 191 | + if err != nil { |
| 192 | + return fmt.Errorf("the specified content file is not available: %q", err) |
| 193 | + } |
| 194 | + request.ImportCustomContentFileBody = ioutil.NopCloser(bytes.NewReader(contents)) |
| 195 | + } |
| 196 | + |
| 197 | + if isOverwrite, ok := s.D.GetOkExists("is_overwrite"); ok { |
| 198 | + tmp := isOverwrite.(bool) |
| 199 | + request.IsOverwrite = &tmp |
| 200 | + } |
| 201 | + |
| 202 | + if namespace, ok := s.D.GetOkExists("namespace"); ok { |
| 203 | + tmp := namespace.(string) |
| 204 | + request.NamespaceName = &tmp |
| 205 | + } |
| 206 | + |
| 207 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "log_analytics") |
| 208 | + |
| 209 | + response, err := s.Client.ImportCustomContent(context.Background(), request) |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + |
| 214 | + s.Res = &response.LogAnalyticsImportCustomContent |
| 215 | + return nil |
| 216 | +} |
| 217 | + |
| 218 | +func (s *LogAnalyticsLogAnalyticsImportCustomContentResourceCrud) SetData() error { |
| 219 | + if s.Res.ChangeList != nil { |
| 220 | + s.D.Set("change_list", []interface{}{LogAnalyticsImportCustomChangeListToMap(s.Res.ChangeList)}) |
| 221 | + } else { |
| 222 | + s.D.Set("change_list", nil) |
| 223 | + } |
| 224 | + |
| 225 | + if s.Res.ContentName != nil { |
| 226 | + s.D.Set("content_name", *s.Res.ContentName) |
| 227 | + } |
| 228 | + |
| 229 | + s.D.Set("field_names", s.Res.FieldNames) |
| 230 | + |
| 231 | + s.D.Set("parser_names", s.Res.ParserNames) |
| 232 | + |
| 233 | + s.D.Set("source_names", s.Res.SourceNames) |
| 234 | + |
| 235 | + return nil |
| 236 | +} |
| 237 | + |
| 238 | +func LogAnalyticsImportCustomChangeListToMap(obj *oci_log_analytics.LogAnalyticsImportCustomChangeList) map[string]interface{} { |
| 239 | + result := map[string]interface{}{} |
| 240 | + |
| 241 | + result["conflict_field_display_names"] = obj.ConflictFieldDisplayNames |
| 242 | + |
| 243 | + result["conflict_parser_names"] = obj.ConflictParserNames |
| 244 | + |
| 245 | + result["conflict_source_names"] = obj.ConflictSourceNames |
| 246 | + |
| 247 | + result["created_field_display_names"] = obj.CreatedFieldDisplayNames |
| 248 | + |
| 249 | + result["created_parser_names"] = obj.CreatedParserNames |
| 250 | + |
| 251 | + result["created_source_names"] = obj.CreatedSourceNames |
| 252 | + |
| 253 | + result["updated_field_display_names"] = obj.UpdatedFieldDisplayNames |
| 254 | + |
| 255 | + result["updated_parser_names"] = obj.UpdatedParserNames |
| 256 | + |
| 257 | + result["updated_source_names"] = obj.UpdatedSourceNames |
| 258 | + |
| 259 | + return result |
| 260 | +} |
0 commit comments