Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: condition status should be string type #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -10,11 +11,7 @@ public class TrustifyStatus {
private List<TrustifyStatusCondition> conditions;

public TrustifyStatus() {
conditions = List.of(
new TrustifyStatusCondition(TrustifyStatusCondition.AVAILABLE, false),
new TrustifyStatusCondition(TrustifyStatusCondition.PROCESSING, false),
new TrustifyStatusCondition(TrustifyStatusCondition.DEGRADED, false)
);
conditions = new ArrayList<>();
}

public List<TrustifyStatusCondition> getConditions() {
Expand All @@ -28,7 +25,7 @@ public void setConditions(List<TrustifyStatusCondition> conditions) {
@JsonIgnore
public void setCondition(TrustifyStatusCondition condition) {
List<TrustifyStatusCondition> conditions = this.conditions.stream()
.filter(item -> !item.type().equals(condition.type()))
.filter(item -> !item.getType().equals(condition.getType()))
.collect(Collectors.toList());
conditions.add(condition);
this.conditions = conditions;
Expand All @@ -37,7 +34,19 @@ public void setCondition(TrustifyStatusCondition condition) {
@JsonIgnore
public boolean isAvailable() {
return this.conditions.stream()
.anyMatch(item -> item.type().equals(TrustifyStatusCondition.AVAILABLE) && Objects.equals(item.status(), true));
.anyMatch(item -> item.getType().equals(TrustifyStatusCondition.Successful) && Objects.equals(item.getStatus(), true));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TrustifyStatus status = (TrustifyStatus) o;
return Objects.equals(getConditions(), status.getConditions());
}

@Override
public int hashCode() {
return Objects.hash(getConditions());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,80 @@
package org.trustify.operator.cdrs.v2alpha1;

public record TrustifyStatusCondition(String type, Boolean status) {
public static final String AVAILABLE = "Available";
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Objects;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class TrustifyStatusCondition {
public static final String Successful = "Successful";
public static final String PROCESSING = "Processing";
public static final String DEGRADED = "Degraded";

public enum Status {
True,
False,
Unknown
}

private String type;
private String status = Status.Unknown.name();

@JsonProperty("type")
public String getType() {
return type;
}

@JsonProperty("type")
public void setType(String type) {
this.type = type;
}

@JsonProperty("status")
public String getStatusString() {
return status;
}

@JsonProperty("status")
public void setStatusString(String status) {
this.status = status;
}

public Boolean getStatus() {
if (status == null || Status.Unknown.name().equals(status)) {
return null;
}
return Status.True.name().equals(status);
}

public void setStatus(Boolean status) {
if (status == null) {
this.status = Status.Unknown.name();
} else if (status) {
this.status = Status.True.name();
} else {
this.status = Status.False.name();
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TrustifyStatusCondition that = (TrustifyStatusCondition) o;
return Objects.equals(getType(), that.getType()) && Objects.equals(getStatus(), that.getStatus());
}

@Override
public int hashCode() {
return Objects.hash(getType(), getStatus());
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"type='" + type + '\'' +
", status=" + status +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ public UpdateControl<Trustify> reconcile(Trustify cr, Context context) {
logger.infof("Trustify {} is ready to be used", cr.getMetadata().getName());
}

TrustifyStatusCondition updatedCondition = new TrustifyStatusCondition(TrustifyStatusCondition.AVAILABLE, true);
cr.getStatus().setCondition(updatedCondition);
TrustifyStatusCondition status = new TrustifyStatusCondition();
status.setType(TrustifyStatusCondition.Successful);
status.setStatus(true);

cr.getStatus().setCondition(status);

return UpdateControl.updateStatus(cr);
} else {
TrustifyStatusCondition updatedCondition = new TrustifyStatusCondition(TrustifyStatusCondition.PROCESSING, true);
cr.getStatus().setCondition(updatedCondition);
TrustifyStatusCondition status = new TrustifyStatusCondition();
status.setType(TrustifyStatusCondition.PROCESSING);
status.setStatus(true);

cr.getStatus().setCondition(status);

final var duration = Duration.ofSeconds(5);
return UpdateControl.updateStatus(cr).rescheduleAfter(duration);
Expand Down