| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.gogrid.predicates; |
| 20 | |
| 21 | import com.google.common.base.Predicate; |
| 22 | import com.google.common.collect.Iterables; |
| 23 | import com.google.inject.Inject; |
| 24 | import org.jclouds.gogrid.domain.Job; |
| 25 | import org.jclouds.gogrid.domain.JobState; |
| 26 | import org.jclouds.gogrid.domain.LoadBalancer; |
| 27 | import org.jclouds.gogrid.options.GetJobListOptions; |
| 28 | import org.jclouds.gogrid.services.GridJobClient; |
| 29 | import org.jclouds.logging.Logger; |
| 30 | |
| 31 | import javax.annotation.Resource; |
| 32 | import javax.inject.Singleton; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
| 37 | * @author Oleksiy Yarmula |
| 38 | */ |
| 39 | @Singleton |
| 40 | public class LoadBalancerLatestJobCompleted implements Predicate<LoadBalancer> { |
| 41 | |
| 42 | protected GridJobClient jobClient; |
| 43 | |
| 44 | @Resource |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | |
| 47 | @Inject |
| 48 | public LoadBalancerLatestJobCompleted(GridJobClient jobClient) { |
| 49 | this.jobClient = jobClient; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean apply(LoadBalancer loadBalancer) { |
| 54 | checkNotNull(loadBalancer, "Load balancer must be a valid instance"); |
| 55 | checkNotNull(loadBalancer.getName(), "Load balancer must be a valid name"); |
| 56 | GetJobListOptions jobOptions = new GetJobListOptions.Builder(). |
| 57 | latestJobForObjectByName(loadBalancer.getName()); |
| 58 | Job latestJob = Iterables.getOnlyElement(jobClient.getJobList(jobOptions)); |
| 59 | return JobState.SUCCEEDED.equals(latestJob.getCurrentState()); |
| 60 | } |
| 61 | } |