|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cloudstack |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 26 | + "github.com/hashicorp/terraform/helper/schema" |
| 27 | +) |
| 28 | + |
| 29 | +func resourceCloudStackSecondaryStorage() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Create: resourceCloudStackSecondaryStorageCreate, |
| 32 | + Read: resourceCloudStackSecondaryStorageRead, |
| 33 | + Delete: resourceCloudStackSecondaryStorageDelete, |
| 34 | + Importer: &schema.ResourceImporter{ |
| 35 | + State: schema.ImportStatePassthrough, |
| 36 | + }, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "name": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Computed: true, |
| 42 | + ForceNew: true, |
| 43 | + }, |
| 44 | + "storage_provider": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + }, |
| 49 | + "url": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + Computed: true, |
| 53 | + ForceNew: true, |
| 54 | + }, |
| 55 | + "zone_id": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Computed: true, |
| 59 | + ForceNew: true, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func resourceCloudStackSecondaryStorageCreate(d *schema.ResourceData, meta interface{}) error { |
| 66 | + cs := meta.(*cloudstack.CloudStackClient) |
| 67 | + |
| 68 | + p := cs.ImageStore.NewAddImageStoreParams(d.Get("storage_provider").(string)) |
| 69 | + if v, ok := d.GetOk("name"); ok { |
| 70 | + p.SetName(v.(string)) |
| 71 | + } |
| 72 | + if v, ok := d.GetOk("url"); ok { |
| 73 | + p.SetUrl(v.(string)) |
| 74 | + } |
| 75 | + if v, ok := d.GetOk("zone_id"); ok { |
| 76 | + p.SetZoneid(v.(string)) |
| 77 | + } |
| 78 | + |
| 79 | + r, err := cs.ImageStore.AddImageStore(p) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + d.SetId(r.Id) |
| 85 | + |
| 86 | + return resourceCloudStackSecondaryStorageRead(d, meta) |
| 87 | +} |
| 88 | + |
| 89 | +func resourceCloudStackSecondaryStorageRead(d *schema.ResourceData, meta interface{}) error { |
| 90 | + cs := meta.(*cloudstack.CloudStackClient) |
| 91 | + |
| 92 | + r, _, err := cs.ImageStore.GetImageStoreByID(d.Id()) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + d.Set("name", r.Name) |
| 98 | + d.Set("storage_provider", r.Providername) |
| 99 | + d.Set("url", r.Url) |
| 100 | + d.Set("zone_id", r.Zoneid) |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func resourceCloudStackSecondaryStorageDelete(d *schema.ResourceData, meta interface{}) error { |
| 106 | + cs := meta.(*cloudstack.CloudStackClient) |
| 107 | + |
| 108 | + _, err := cs.ImageStore.DeleteImageStore(cs.ImageStore.NewDeleteImageStoreParams(d.Id())) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("Error deleting secondary storage: %s", err) |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
0 commit comments