import (
"context"
"fmt"
"io"
talent "cloud.google.com/go/talent/apiv4beta1"
"cloud.google.com/go/talent/apiv4beta1/talentpb"
"github.com/gofrs/uuid"
money "google.golang.org/genproto/googleapis/type/money"
)
// createJobWithCustomAttributes creates a job with custom attributes.
func createJobWithCustomAttributes(w io.Writer, projectID, companyID, jobTitle string) (*talentpb.Job, error) {
ctx := context.Background()
// Initialize a job service client.
c, err := talent.NewJobClient(ctx)
if err != nil {
return nil, fmt.Errorf("talent.NewJobClient: %w", err)
}
defer c.Close()
// requisitionID shoud be the unique ID in your system
requisitionID := fmt.Sprintf("job-with-custom-attribute-%s", uuid.Must(uuid.NewV4()).String())
jobToCreate := &talentpb.Job{
Company: fmt.Sprintf("projects/%s/companies/%s", projectID, companyID),
RequisitionId: requisitionID,
Title: jobTitle,
ApplicationInfo: &talentpb.Job_ApplicationInfo{
Uris: []string{"https://googlesample.com/career"},
},
Description: "Design, devolop, test, deploy, maintain and improve software.",
LanguageCode: "en-US",
PromotionValue: 2,
EmploymentTypes: []talentpb.EmploymentType{talentpb.EmploymentType_FULL_TIME},
Addresses: []string{"Mountain View, CA"},
CustomAttributes: map[string]*talentpb.CustomAttribute{
"someFieldString": {
Filterable: true,
StringValues: []string{"someStrVal"},
},
"someFieldLong": {
Filterable: true,
LongValues: []int64{900},
},
},
CompensationInfo: &talentpb.CompensationInfo{
Entries: []*talentpb.CompensationInfo_CompensationEntry{
{
Type: talentpb.CompensationInfo_BASE,
Unit: talentpb.CompensationInfo_HOURLY,
CompensationAmount: &talentpb.CompensationInfo_CompensationEntry_Amount{
Amount: &money.Money{
CurrencyCode: "USD",
Units: 1,
},
},
},
},
},
}
// Construct a createJob request.
req := &talentpb.CreateJobRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
Job: jobToCreate,
}
resp, err := c.CreateJob(ctx, req)
if err != nil {
return nil, fmt.Errorf("CreateJob: %w", err)
}
fmt.Fprintf(w, "Created job with custom attributres: %q\n", resp.GetName())
fmt.Fprintf(w, "Custom long field has value: %v\n", resp.GetCustomAttributes()["someFieldLong"].GetLongValues())
return resp, nil
}