|
| 1 | +package datafactory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" |
| 9 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" |
| 10 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" |
| 11 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" |
| 12 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse" |
| 13 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/validate" |
| 14 | + networkValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/validate" |
| 15 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk" |
| 16 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" |
| 17 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" |
| 18 | +) |
| 19 | + |
| 20 | +func resourceDataFactoryManagedPrivateEndpoint() *pluginsdk.Resource { |
| 21 | + return &pluginsdk.Resource{ |
| 22 | + Create: resourceDataFactoryManagedPrivateEndpointCreate, |
| 23 | + Read: resourceDataFactoryManagedPrivateEndpointRead, |
| 24 | + Delete: resourceDataFactoryManagedPrivateEndpointDelete, |
| 25 | + |
| 26 | + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { |
| 27 | + _, err := parse.ManagedPrivateEndpointID(id) |
| 28 | + return err |
| 29 | + }), |
| 30 | + |
| 31 | + Timeouts: &pluginsdk.ResourceTimeout{ |
| 32 | + Create: pluginsdk.DefaultTimeout(30 * time.Minute), |
| 33 | + Read: pluginsdk.DefaultTimeout(5 * time.Minute), |
| 34 | + Delete: pluginsdk.DefaultTimeout(30 * time.Minute), |
| 35 | + }, |
| 36 | + |
| 37 | + Schema: map[string]*pluginsdk.Schema{ |
| 38 | + "name": { |
| 39 | + Type: pluginsdk.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + ValidateFunc: validate.DataFactoryManagedPrivateEndpointName(), |
| 43 | + }, |
| 44 | + |
| 45 | + "data_factory_id": { |
| 46 | + Type: pluginsdk.TypeString, |
| 47 | + Required: true, |
| 48 | + ForceNew: true, |
| 49 | + ValidateFunc: validate.DataFactoryID, |
| 50 | + }, |
| 51 | + |
| 52 | + "target_resource_id": { |
| 53 | + Type: pluginsdk.TypeString, |
| 54 | + Required: true, |
| 55 | + ForceNew: true, |
| 56 | + ValidateFunc: azure.ValidateResourceID, |
| 57 | + }, |
| 58 | + |
| 59 | + "subresource_name": { |
| 60 | + Type: pluginsdk.TypeString, |
| 61 | + Required: true, |
| 62 | + ForceNew: true, |
| 63 | + ValidateFunc: networkValidate.PrivateLinkSubResourceName, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func resourceDataFactoryManagedPrivateEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) error { |
| 70 | + client := meta.(*clients.Client).DataFactory.ManagedPrivateEndpointsClient |
| 71 | + managedVirtualNetworksClient := meta.(*clients.Client).DataFactory.ManagedVirtualNetworksClient |
| 72 | + subscriptionId := meta.(*clients.Client).Account.SubscriptionId |
| 73 | + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + dataFactoryId, err := parse.DataFactoryID(d.Get("data_factory_id").(string)) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + managedVirtualNetworkName, err := getManagedVirtualNetworkName(ctx, managedVirtualNetworksClient, dataFactoryId.ResourceGroup, dataFactoryId.FactoryName) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + if managedVirtualNetworkName == nil { |
| 86 | + return fmt.Errorf("managed Private endpoints are only available after managed virtual network for %s is enabled", dataFactoryId) |
| 87 | + } |
| 88 | + |
| 89 | + id := parse.NewManagedPrivateEndpointID(subscriptionId, dataFactoryId.ResourceGroup, dataFactoryId.FactoryName, *managedVirtualNetworkName, d.Get("name").(string)) |
| 90 | + existing, err := getManagedPrivateEndpoint(ctx, client, id.ResourceGroup, id.FactoryName, *managedVirtualNetworkName, id.Name) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) |
| 93 | + } |
| 94 | + if existing != nil { |
| 95 | + return tf.ImportAsExistsError("azurerm_data_factory_managed_private_endpoint", id.ID()) |
| 96 | + } |
| 97 | + |
| 98 | + managedPrivateEndpoint := datafactory.ManagedPrivateEndpointResource{ |
| 99 | + Properties: &datafactory.ManagedPrivateEndpoint{ |
| 100 | + PrivateLinkResourceID: utils.String(d.Get("target_resource_id").(string)), |
| 101 | + GroupID: utils.String(d.Get("subresource_name").(string)), |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.ManagedVirtualNetworkName, id.Name, managedPrivateEndpoint, ""); err != nil { |
| 106 | + return fmt.Errorf("creating %s: %+v", id, err) |
| 107 | + } |
| 108 | + |
| 109 | + d.SetId(id.ID()) |
| 110 | + |
| 111 | + return resourceDataFactoryManagedPrivateEndpointRead(d, meta) |
| 112 | +} |
| 113 | + |
| 114 | +func resourceDataFactoryManagedPrivateEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error { |
| 115 | + client := meta.(*clients.Client).DataFactory.ManagedPrivateEndpointsClient |
| 116 | + subscriptionId := meta.(*clients.Client).Account.SubscriptionId |
| 117 | + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) |
| 118 | + defer cancel() |
| 119 | + |
| 120 | + id, err := parse.ManagedPrivateEndpointID(d.Id()) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.ManagedVirtualNetworkName, id.Name, "") |
| 126 | + if err != nil { |
| 127 | + if utils.ResponseWasNotFound(resp.Response) { |
| 128 | + d.SetId("") |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + return fmt.Errorf("retrieving %s: %+v", id, err) |
| 133 | + } |
| 134 | + |
| 135 | + d.Set("name", id.Name) |
| 136 | + d.Set("data_factory_id", parse.NewDataFactoryID(subscriptionId, id.ResourceGroup, id.FactoryName).ID()) |
| 137 | + |
| 138 | + if props := resp.Properties; props != nil { |
| 139 | + d.Set("target_resource_id", props.PrivateLinkResourceID) |
| 140 | + d.Set("subresource_name", props.GroupID) |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func resourceDataFactoryManagedPrivateEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) error { |
| 147 | + client := meta.(*clients.Client).DataFactory.ManagedPrivateEndpointsClient |
| 148 | + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) |
| 149 | + defer cancel() |
| 150 | + |
| 151 | + id, err := parse.ManagedPrivateEndpointID(d.Id()) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + if _, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.ManagedVirtualNetworkName, id.Name); err != nil { |
| 157 | + return fmt.Errorf("deleting %s: %+v", id, err) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +// if ManagedPrivateEndpoint not exist, get rest api will return 400 bad request |
| 164 | +// invoke list rets api and then filter by name |
| 165 | +func getManagedPrivateEndpoint(ctx context.Context, client *datafactory.ManagedPrivateEndpointsClient, resourceGroupName, factoryName, managedVirtualNetworkName, name string) (*datafactory.ManagedPrivateEndpointResource, error) { |
| 166 | + iter, err := client.ListByFactoryComplete(ctx, resourceGroupName, factoryName, managedVirtualNetworkName) |
| 167 | + if err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + for iter.NotDone() { |
| 171 | + managedPrivateEndpoint := iter.Value() |
| 172 | + if managedPrivateEndpoint.Name != nil && *managedPrivateEndpoint.Name == name { |
| 173 | + return &managedPrivateEndpoint, nil |
| 174 | + } |
| 175 | + |
| 176 | + if err := iter.NextWithContext(ctx); err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + } |
| 180 | + return nil, nil |
| 181 | +} |
0 commit comments