|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionRequest; |
| 22 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 23 | +import org.elasticsearch.client.ml.job.config.Job; |
| 24 | +import org.elasticsearch.client.ml.job.process.ModelSnapshot; |
| 25 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +/** |
| 33 | + * A request to update information about an existing model snapshot for a given job |
| 34 | + */ |
| 35 | +public class UpdateModelSnapshotRequest extends ActionRequest implements ToXContentObject { |
| 36 | + |
| 37 | + |
| 38 | + public static final ConstructingObjectParser<UpdateModelSnapshotRequest, Void> PARSER = new ConstructingObjectParser<>( |
| 39 | + "update_model_snapshot_request", a -> new UpdateModelSnapshotRequest((String) a[0], (String) a[1])); |
| 40 | + |
| 41 | + |
| 42 | + static { |
| 43 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); |
| 44 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), ModelSnapshot.SNAPSHOT_ID); |
| 45 | + PARSER.declareStringOrNull(UpdateModelSnapshotRequest::setDescription, ModelSnapshot.DESCRIPTION); |
| 46 | + PARSER.declareBoolean(UpdateModelSnapshotRequest::setRetain, ModelSnapshot.RETAIN); |
| 47 | + } |
| 48 | + |
| 49 | + private final String jobId; |
| 50 | + private String snapshotId; |
| 51 | + private String description; |
| 52 | + private Boolean retain; |
| 53 | + |
| 54 | + /** |
| 55 | + * Constructs a request to update information for a snapshot of given job |
| 56 | + * @param jobId id of the job from which to retrieve results |
| 57 | + * @param snapshotId id of the snapshot from which to retrieve results |
| 58 | + */ |
| 59 | + public UpdateModelSnapshotRequest(String jobId, String snapshotId) { |
| 60 | + this.jobId = Objects.requireNonNull(jobId, "[" + Job.ID + "] must not be null"); |
| 61 | + this.snapshotId = Objects.requireNonNull(snapshotId, "[" + ModelSnapshot.SNAPSHOT_ID + "] must not be null"); |
| 62 | + } |
| 63 | + |
| 64 | + public String getJobId() { |
| 65 | + return jobId; |
| 66 | + } |
| 67 | + |
| 68 | + public String getSnapshotId() { |
| 69 | + return snapshotId; |
| 70 | + } |
| 71 | + |
| 72 | + public String getDescription() { |
| 73 | + return description; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * The new description of the snapshot. |
| 78 | + * @param description the updated snapshot description |
| 79 | + */ |
| 80 | + public void setDescription(String description) { |
| 81 | + this.description = description; |
| 82 | + } |
| 83 | + |
| 84 | + public Boolean getRetain() { |
| 85 | + return retain; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * The new value of the "retain" property of the snapshot |
| 90 | + * @param retain the updated retain property |
| 91 | + */ |
| 92 | + public void setRetain(boolean retain) { |
| 93 | + this.retain = retain; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public ActionRequestValidationException validate() { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 103 | + builder.startObject(); |
| 104 | + builder.field(Job.ID.getPreferredName(), jobId); |
| 105 | + builder.field(ModelSnapshot.SNAPSHOT_ID.getPreferredName(), snapshotId); |
| 106 | + if (description != null) { |
| 107 | + builder.field(ModelSnapshot.DESCRIPTION.getPreferredName(), description); |
| 108 | + } |
| 109 | + if (retain != null) { |
| 110 | + builder.field(ModelSnapshot.RETAIN.getPreferredName(), retain); |
| 111 | + } |
| 112 | + builder.endObject(); |
| 113 | + return builder; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean equals(Object obj) { |
| 118 | + if (obj == null) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + if (getClass() != obj.getClass()) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + UpdateModelSnapshotRequest request = (UpdateModelSnapshotRequest) obj; |
| 125 | + return Objects.equals(jobId, request.jobId) |
| 126 | + && Objects.equals(snapshotId, request.snapshotId) |
| 127 | + && Objects.equals(description, request.description) |
| 128 | + && Objects.equals(retain, request.retain); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int hashCode() { |
| 133 | + return Objects.hash(jobId, snapshotId, description, retain); |
| 134 | + } |
| 135 | +} |
0 commit comments