func()

in ec/ecresource/deploymentresource/create.go [30:107]


func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	if !r.ready(&resp.Diagnostics) {
		return
	}

	var config v2.DeploymentTF
	diags := req.Config.Get(ctx, &config)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	var plan v2.DeploymentTF
	diags = req.Plan.Get(ctx, &plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	request, diags := plan.CreateRequest(ctx, r.client)
	if diags.HasError() {
		resp.Diagnostics.Append(diags...)
		return
	}

	requestId := deploymentapi.RequestID(plan.RequestId.ValueString())

	res, err := deploymentapi.Create(deploymentapi.CreateParams{
		API:       r.client,
		RequestID: requestId,
		Request:   request,
		Overrides: &deploymentapi.PayloadOverrides{
			Name:    plan.Name.ValueString(),
			Version: plan.Version.ValueString(),
			Region:  plan.Region.ValueString(),
		},
	})

	if err != nil {
		resp.Diagnostics.AddError("failed creating deployment", err.Error())
		resp.Diagnostics.AddError("failed creating deployment", newCreationError(requestId).Error())
		return
	}

	// Set the ID immediately so the deployment is managed by Terraform.
	// If the rest of this fails the deployment will be tainted and recreated,
	// but that's preferable to leaving an unmanaged resource.
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), *res.ID)...)
	if resp.Diagnostics.HasError() {
		return
	}

	if err := WaitForPlanCompletion(r.client, *res.ID); err != nil {
		resp.Diagnostics.AddError("failed tracking create progress", err.Error())
		resp.Diagnostics.AddError("failed tracking create progress", newCreationError(requestId).Error())
		return
	}

	tflog.Trace(ctx, "created deployment resource")

	resp.Diagnostics.Append(v2.HandleRemoteClusters(ctx, r.client, *res.ID, plan.Elasticsearch)...)

	filters := []string{}
	if request.Settings != nil && request.Settings.TrafficFilterSettings != nil && request.Settings.TrafficFilterSettings.Rulesets != nil {
		filters = request.Settings.TrafficFilterSettings.Rulesets
	}

	deployment, diags := r.read(ctx, *res.ID, nil, &plan, res.Resources, filters, nil)
	updatePrivateStateTrafficFilters(ctx, resp.Private, filters)

	resp.Diagnostics.Append(diags...)
	if deployment == nil {
		resp.Diagnostics.AddError("cannot read just created resource", "")
		return
	}

	resp.Diagnostics.Append(resp.State.Set(ctx, deployment)...)
}