|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 6 | + "log" |
| 7 | +) |
| 8 | + |
| 9 | +func resourceSumologicDataForwardingDestination() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + |
| 12 | + Create: resourceSumologicDataForwardingDestinationCreate, |
| 13 | + Read: resourceSumologicDataForwardingDestinationRead, |
| 14 | + Update: resourceSumologicDataForwardingDestinationUpdate, |
| 15 | + Delete: resourceSumologicDataForwardingDestinationDelete, |
| 16 | + Importer: &schema.ResourceImporter{ |
| 17 | + State: schema.ImportStatePassthrough, |
| 18 | + }, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + |
| 22 | + "destination_name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ValidateFunc: validation.StringLenBetween(1, 255), |
| 26 | + }, |
| 27 | + "description": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Optional: true, |
| 30 | + }, |
| 31 | + "bucket_name": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + }, |
| 35 | + "authentication": { |
| 36 | + Type: schema.TypeList, |
| 37 | + Required: true, |
| 38 | + MaxItems: 1, |
| 39 | + Elem: &schema.Resource{ |
| 40 | + Schema: map[string]*schema.Schema{ |
| 41 | + "type": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + ValidateFunc: validation.StringInSlice([]string{"RoleBased", "AccessKey"}, false), |
| 45 | + }, |
| 46 | + "role_arn": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + }, |
| 50 | + "access_key": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + }, |
| 54 | + "secret_key": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + "s3_region": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + }, |
| 65 | + "s3_server_side_encryption": { |
| 66 | + Type: schema.TypeBool, |
| 67 | + Optional: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +func resourceSumologicDataForwardingDestinationCreate(d *schema.ResourceData, meta interface{}) error { |
| 75 | + c := meta.(*Client) |
| 76 | + |
| 77 | + if d.Id() == "" { |
| 78 | + dataForwardingDestination := resourceToDataForwardingDestination(d) |
| 79 | + createdDataForwardingDestination, err := c.CreateDataForwardingDestination(dataForwardingDestination) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + d.SetId(createdDataForwardingDestination.ID) |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + return resourceSumologicDataForwardingDestinationUpdate(d, meta) |
| 90 | +} |
| 91 | + |
| 92 | +func resourceSumologicDataForwardingDestinationUpdate(d *schema.ResourceData, meta interface{}) error { |
| 93 | + |
| 94 | + dataForwardingDestination := resourceToDataForwardingDestination(d) |
| 95 | + |
| 96 | + c := meta.(*Client) |
| 97 | + err := c.UpdateDataForwardingDestination(dataForwardingDestination) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + return resourceSumologicDataForwardingDestinationRead(d, meta) |
| 104 | +} |
| 105 | + |
| 106 | +func resourceSumologicDataForwardingDestinationRead(d *schema.ResourceData, meta interface{}) error { |
| 107 | + c := meta.(*Client) |
| 108 | + |
| 109 | + id := d.Id() |
| 110 | + dataForwardingDestination, err := c.getDataForwardingDestination(id) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + if dataForwardingDestination == nil { |
| 117 | + log.Printf("[WARN] Data Forwarding destination not found, removing from state: %v - %v", id, err) |
| 118 | + d.SetId("") |
| 119 | + |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + d.Set("destination_name", dataForwardingDestination.DestinationName) |
| 124 | + d.Set("description", dataForwardingDestination.Description) |
| 125 | + d.Set("bucket_name", dataForwardingDestination.BucketName) |
| 126 | + d.Set("S3_region", dataForwardingDestination.S3Region) |
| 127 | + d.Set("S3_server_side_encryption", dataForwardingDestination.S3ServerSideEncryption) |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func resourceSumologicDataForwardingDestinationDelete(d *schema.ResourceData, meta interface{}) error { |
| 133 | + c := meta.(*Client) |
| 134 | + id := d.Id() |
| 135 | + |
| 136 | + return c.DeleteDataForwardingDestination(id) |
| 137 | +} |
| 138 | + |
| 139 | +func resourceToDataForwardingDestination(d *schema.ResourceData) DataForwardingDestination { |
| 140 | + |
| 141 | + authentication := extractAuthenticationDetails(d.Get("authentication").([]interface{})) |
| 142 | + |
| 143 | + return DataForwardingDestination{ |
| 144 | + ID: d.Id(), |
| 145 | + DestinationName: d.Get("destination_name").(string), |
| 146 | + Description: d.Get("description").(string), |
| 147 | + BucketName: d.Get("bucket_name").(string), |
| 148 | + AccessMethod: authentication["type"].(string), |
| 149 | + AccessKey: authentication["access_key"].(string), |
| 150 | + SecretKey: authentication["secret_key"].(string), |
| 151 | + RoleArn: authentication["role_arn"].(string), |
| 152 | + S3Region: d.Get("s3_region").(string), |
| 153 | + S3ServerSideEncryption: d.Get("s3_server_side_encryption").(bool), |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func extractAuthenticationDetails(authenticationList []interface{}) map[string]interface{} { |
| 158 | + if len(authenticationList) > 0 { |
| 159 | + authMap := authenticationList[0].(map[string]interface{}) |
| 160 | + return authMap |
| 161 | + } |
| 162 | + return map[string]interface{}{ |
| 163 | + "type": nil, |
| 164 | + "role_arn": nil, |
| 165 | + "access_key": nil, |
| 166 | + "secret_key": nil, |
| 167 | + } |
| 168 | +} |
0 commit comments