func()

in ec/ecresource/projectresource/elasticsearch.go [112:170]


func (es elasticsearchApi) Create(ctx context.Context, model resource_elasticsearch_project.ElasticsearchProjectModel) (resource_elasticsearch_project.ElasticsearchProjectModel, diag.Diagnostics) {
	createBody := serverless.CreateElasticsearchProjectRequest{
		Name:     model.Name.ValueString(),
		RegionId: model.RegionId.ValueString(),
	}

	if model.Alias.ValueString() != "" {
		createBody.Alias = model.Alias.ValueStringPointer()
	}

	if model.OptimizedFor.ValueString() != "" {
		createBody.OptimizedFor = (*serverless.ElasticsearchOptimizedFor)(model.OptimizedFor.ValueStringPointer())
	}

	if util.IsKnown(model.SearchLake) {
		createBody.SearchLake = &serverless.ElasticsearchSearchLake{}

		if util.IsKnown(model.SearchLake.BoostWindow) {
			boostWindow := int(model.SearchLake.BoostWindow.ValueInt64())
			createBody.SearchLake.BoostWindow = &boostWindow
		}

		if util.IsKnown(model.SearchLake.SearchPower) {
			searchPower := int(model.SearchLake.SearchPower.ValueInt64())
			createBody.SearchLake.SearchPower = &searchPower
		}
	}

	resp, err := es.client.CreateElasticsearchProjectWithResponse(ctx, createBody)
	if err != nil {
		return model, diag.Diagnostics{
			diag.NewErrorDiagnostic(err.Error(), err.Error()),
		}
	}

	if resp.JSON201 == nil {
		return model, diag.Diagnostics{
			diag.NewErrorDiagnostic(
				"Failed to create elasticsearch_project",
				fmt.Sprintf("The API request failed with: %d %s\n%s",
					resp.StatusCode(),
					resp.Status(),
					resp.Body),
			),
		}
	}

	model.Id = types.StringValue(resp.JSON201.Id)

	creds, diags := resource_elasticsearch_project.NewCredentialsValue(
		model.Credentials.AttributeTypes(ctx),
		map[string]attr.Value{
			"username": types.StringValue(resp.JSON201.Credentials.Username),
			"password": types.StringValue(resp.JSON201.Credentials.Password),
		},
	)
	model.Credentials = creds
	return model, diags
}