-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cf): "Run Job" stage support for CloudFoundry (#3789)
- Loading branch information
Pierre Delagrave
authored and
Jammy Louie
committed
Jun 21, 2019
1 parent
abc102e
commit a8e26cc
Showing
13 changed files
with
481 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...oudfoundry/src/main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/Tasks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2019 Pivotal, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.cloudfoundry.client; | ||
|
||
import static com.netflix.spinnaker.clouddriver.cloudfoundry.client.CloudFoundryClientUtils.safelyCall; | ||
|
||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.api.TaskService; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.CreateTask; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.Task; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class Tasks { | ||
private final TaskService api; | ||
|
||
public Task createTask(String applicationGuid, String command, String name) { | ||
CreateTask createTask = new CreateTask(name, command); | ||
|
||
return safelyCall(() -> api.createTask(applicationGuid, createTask)) | ||
.orElseThrow(ResourceNotFoundException::new); | ||
} | ||
|
||
public Task getTask(String id) { | ||
return safelyCall(() -> api.getTask(id)).orElseThrow(ResourceNotFoundException::new); | ||
} | ||
|
||
public Task cancelTask(String id) { | ||
return safelyCall(() -> api.cancelTask(id, "")).orElseThrow(ResourceNotFoundException::new); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../src/main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/api/TaskService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2019 Pivotal, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.cloudfoundry.client.api; | ||
|
||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.CreateTask; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.Task; | ||
import retrofit.http.Body; | ||
import retrofit.http.GET; | ||
import retrofit.http.POST; | ||
import retrofit.http.Path; | ||
|
||
public interface TaskService { | ||
|
||
@POST("/v3/apps/{guid}/tasks") | ||
Task createTask(@Path("guid") String guid, @Body CreateTask body); | ||
|
||
@GET("/v3/tasks/{guid}") | ||
Task getTask(@Path("guid") String guid); | ||
|
||
@POST("/v3/tasks/{guid}/actions/cancel") | ||
Task cancelTask(@Path("guid") String guid, @Body Object emptyBody); | ||
} |
26 changes: 26 additions & 0 deletions
26
.../main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/model/v3/CreateTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2019 Pivotal, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3; | ||
|
||
import javax.annotation.Nullable; | ||
import lombok.Value; | ||
|
||
@Value | ||
public class CreateTask { | ||
@Nullable private final String name; | ||
private final String command; | ||
} |
35 changes: 35 additions & 0 deletions
35
...ry/src/main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/model/v3/Task.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2019 Pivotal, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3; | ||
|
||
import java.time.ZonedDateTime; | ||
import lombok.Value; | ||
|
||
@Value | ||
public class Task { | ||
private final String guid; | ||
private final String name; | ||
private final State state; | ||
private final ZonedDateTime createdAt; | ||
private final ZonedDateTime updatedAt; | ||
|
||
public enum State { | ||
SUCCEEDED, | ||
RUNNING, | ||
FAILED | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/job/CloudFoundryJobProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2019 Pivotal, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.cloudfoundry.job; | ||
|
||
import com.netflix.spinnaker.clouddriver.cloudfoundry.CloudFoundryCloudProvider; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.Task; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.security.CloudFoundryCredentials; | ||
import com.netflix.spinnaker.clouddriver.model.JobProvider; | ||
import com.netflix.spinnaker.clouddriver.security.AccountCredentials; | ||
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CloudFoundryJobProvider implements JobProvider<CloudFoundryJobStatus> { | ||
|
||
@Getter private String platform = CloudFoundryCloudProvider.ID; | ||
private final AccountCredentialsProvider accountCredentialsProvider; | ||
|
||
public CloudFoundryJobProvider(AccountCredentialsProvider accountCredentialsProvider) { | ||
this.accountCredentialsProvider = accountCredentialsProvider; | ||
} | ||
|
||
@Override | ||
public CloudFoundryJobStatus collectJob(String account, String location, String id) { | ||
AccountCredentials credentials = accountCredentialsProvider.getCredentials(account); | ||
if (!(credentials instanceof CloudFoundryCredentials)) { | ||
return null; | ||
} | ||
|
||
Task task = ((CloudFoundryCredentials) credentials).getClient().getTasks().getTask(id); | ||
return CloudFoundryJobStatus.fromTask(task, account, location); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getFileContents( | ||
String account, String location, String id, String fileName) { | ||
throw new NotImplementedException(""); | ||
} | ||
|
||
@Override | ||
public void cancelJob(String account, String location, String taskGuid) { | ||
AccountCredentials credentials = accountCredentialsProvider.getCredentials(account); | ||
if (!(credentials instanceof CloudFoundryCredentials)) { | ||
return; | ||
} | ||
|
||
((CloudFoundryCredentials) credentials).getClient().getTasks().cancelTask(taskGuid); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...c/main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/job/CloudFoundryJobStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2019 Pivotal, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.cloudfoundry.job; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.CloudFoundryCloudProvider; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.Task; | ||
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.v3.Task.State; | ||
import com.netflix.spinnaker.clouddriver.model.JobState; | ||
import com.netflix.spinnaker.clouddriver.model.JobStatus; | ||
import java.io.Serializable; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = false) | ||
@Builder | ||
@JsonDeserialize(builder = CloudFoundryJobStatus.CloudFoundryJobStatusBuilder.class) | ||
public class CloudFoundryJobStatus implements JobStatus { | ||
@Nullable private String name; | ||
|
||
private String account; | ||
|
||
private String id; | ||
|
||
private String location; | ||
|
||
private final String provider = CloudFoundryCloudProvider.ID; | ||
|
||
private JobState jobState; | ||
|
||
private Long createdTime; | ||
|
||
@Nullable private Long completedTime; | ||
|
||
@Override | ||
public Map<String, ? extends Serializable> getCompletionDetails() { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
public static CloudFoundryJobStatus fromTask(Task task, String account, String location) { | ||
State state = task.getState(); | ||
CloudFoundryJobStatusBuilder builder = CloudFoundryJobStatus.builder(); | ||
switch (state) { | ||
case FAILED: | ||
builder.jobState(JobState.Failed); | ||
builder.completedTime(task.getUpdatedAt().toInstant().toEpochMilli()); | ||
break; | ||
case RUNNING: | ||
builder.jobState(JobState.Running); | ||
break; | ||
case SUCCEEDED: | ||
builder.jobState(JobState.Succeeded); | ||
builder.completedTime(task.getUpdatedAt().toInstant().toEpochMilli()); | ||
break; | ||
default: | ||
builder.jobState(JobState.Unknown); | ||
} | ||
return builder | ||
.name(task.getName()) | ||
.account(account) | ||
.id(task.getGuid()) | ||
.location(location) | ||
.createdTime(task.getCreatedAt().toInstant().toEpochMilli()) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.