|
| 1 | +package resource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "terraform-provider-plural/internal/client" |
| 9 | + "terraform-provider-plural/internal/common" |
| 10 | + customvalidator "terraform-provider-plural/internal/validator" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 19 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 20 | + console "github.com/pluralsh/console/go/client" |
| 21 | + "k8s.io/apimachinery/pkg/util/wait" |
| 22 | +) |
| 23 | + |
| 24 | +type serviceWait struct { |
| 25 | + Cluster types.String `tfsdk:"cluster"` |
| 26 | + Service types.String `tfsdk:"service"` |
| 27 | + Warmup types.String `tfsdk:"warmup"` |
| 28 | + Duration types.String `tfsdk:"duration"` |
| 29 | +} |
| 30 | + |
| 31 | +func (in *serviceWait) ParseWarmup() (time.Duration, error) { |
| 32 | + return time.ParseDuration(in.Warmup.ValueString()) |
| 33 | +} |
| 34 | + |
| 35 | +func (in *serviceWait) ParseDuration() (time.Duration, error) { |
| 36 | + return time.ParseDuration(in.Duration.ValueString()) |
| 37 | +} |
| 38 | + |
| 39 | +var _ resource.ResourceWithConfigure = &serviceWaitResource{} |
| 40 | + |
| 41 | +func NewServiceWaitResource() resource.Resource { |
| 42 | + return &serviceWaitResource{} |
| 43 | +} |
| 44 | + |
| 45 | +type serviceWaitResource struct { |
| 46 | + client *client.Client |
| 47 | +} |
| 48 | + |
| 49 | +func (in *serviceWaitResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { |
| 50 | + response.TypeName = request.ProviderTypeName + "_service_wait" |
| 51 | +} |
| 52 | + |
| 53 | +func (in *serviceWaitResource) Schema(_ context.Context, _ resource.SchemaRequest, response *resource.SchemaResponse) { |
| 54 | + response.Schema = schema.Schema{ |
| 55 | + Attributes: map[string]schema.Attribute{ |
| 56 | + "cluster": schema.StringAttribute{ |
| 57 | + Description: "Handle of the cluster where the service is deployed.", |
| 58 | + MarkdownDescription: "Handle of the cluster where the service is deployed.", |
| 59 | + Required: true, |
| 60 | + Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, |
| 61 | + }, |
| 62 | + "service": schema.StringAttribute{ |
| 63 | + Description: "Name the service deployment that should be checked.", |
| 64 | + MarkdownDescription: "Name the service deployment that should be checked.", |
| 65 | + Required: true, |
| 66 | + Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, |
| 67 | + }, |
| 68 | + "warmup": schema.StringAttribute{ |
| 69 | + Description: "Initial delay before checking the service deployment health. Defaults to 5 minutes.", |
| 70 | + MarkdownDescription: "Initial delay before checking the service deployment health. Defaults to 5 minutes.", |
| 71 | + Optional: true, |
| 72 | + Computed: true, |
| 73 | + Default: stringdefault.StaticString("5m"), |
| 74 | + Validators: []validator.String{customvalidator.Duration()}, |
| 75 | + }, |
| 76 | + "duration": schema.StringAttribute{ |
| 77 | + Description: "Maximum duration to wait for the service deployment to become healthy. Minimum 1 minute. Defaults to 10 minutes.", |
| 78 | + MarkdownDescription: "Maximum duration to wait for the service deployment to become healthy. Minimum 1 minute. Defaults to 10 minutes.", |
| 79 | + Optional: true, |
| 80 | + Computed: true, |
| 81 | + Default: stringdefault.StaticString("10m"), |
| 82 | + Validators: []validator.String{customvalidator.MinDuration(time.Minute)}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (in *serviceWaitResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 89 | + if request.ProviderData == nil { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + data, ok := request.ProviderData.(*common.ProviderData) |
| 94 | + if !ok { |
| 95 | + response.Diagnostics.AddError( |
| 96 | + "Unexpected Project Resource Configure Type", |
| 97 | + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", request.ProviderData), |
| 98 | + ) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + in.client = data.Client |
| 103 | +} |
| 104 | + |
| 105 | +func (in *serviceWaitResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { |
| 106 | + data := new(serviceWait) |
| 107 | + response.Diagnostics.Append(request.Plan.Get(ctx, data)...) |
| 108 | + if response.Diagnostics.HasError() { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + if err := in.Wait(ctx, data); err != nil { |
| 113 | + response.Diagnostics.AddError("Client Error", fmt.Sprintf("Got error while waiting for service: %s", err)) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + response.Diagnostics.Append(response.State.Set(ctx, &data)...) |
| 118 | +} |
| 119 | + |
| 120 | +func (in *serviceWaitResource) Read(_ context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) { |
| 121 | + // Ignore. |
| 122 | +} |
| 123 | + |
| 124 | +func (in *serviceWaitResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 125 | + var data serviceWait |
| 126 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 127 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 128 | +} |
| 129 | + |
| 130 | +func (in *serviceWaitResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) { |
| 131 | + // Ignore. |
| 132 | +} |
| 133 | + |
| 134 | +func (in *serviceWaitResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 135 | + resource.ImportStatePassthroughID(ctx, path.Root("service_id"), req, resp) |
| 136 | +} |
| 137 | + |
| 138 | +func (in *serviceWaitResource) Wait(ctx context.Context, data *serviceWait) error { |
| 139 | + warmup, err := data.ParseWarmup() |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("unable to parse warmup duration, got error: %s", err.Error()) |
| 142 | + } |
| 143 | + |
| 144 | + duration, err := data.ParseDuration() |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("unable to parse duration, got error: %s", err.Error()) |
| 147 | + } |
| 148 | + |
| 149 | + tflog.Info(ctx, fmt.Sprintf("waiting for warmup period of %s before starting health checks...", warmup)) |
| 150 | + time.Sleep(warmup) |
| 151 | + tflog.Info(ctx, "warmup period completed, starting health checks") |
| 152 | + |
| 153 | + if err = wait.PollUntilContextTimeout(context.Background(), 30*time.Second, duration, true, func(pollCtx context.Context) (done bool, err error) { |
| 154 | + service, err := in.client.GetServiceDeploymentByHandle(pollCtx, data.Cluster.ValueString(), data.Service.ValueString()) |
| 155 | + if err != nil { |
| 156 | + tflog.Warn(ctx, fmt.Sprintf("failed to get service %s, got error: %s", data.Service.ValueString(), err.Error())) |
| 157 | + return false, nil |
| 158 | + } |
| 159 | + |
| 160 | + tflog.Debug(ctx, fmt.Sprintf("service %s is %s", service.ServiceDeployment.ID, service.ServiceDeployment.Status)) |
| 161 | + return service.ServiceDeployment.Status == console.ServiceDeploymentStatusHealthy, nil |
| 162 | + }); err != nil { |
| 163 | + tflog.Warn(ctx, fmt.Sprintf("service %s did not become healthy within %s, got error: %s", data.Service.ValueString(), duration, err.Error())) |
| 164 | + return fmt.Errorf("service %s did not become healthy within %s, got error: %s", data.Service.ValueString(), duration, err.Error()) |
| 165 | + } |
| 166 | + |
| 167 | + tflog.Info(ctx, fmt.Sprintf("service %s health check completed successfully", data.Service.ValueString())) |
| 168 | + return nil |
| 169 | +} |
0 commit comments