All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.huawei.openstack4j.openstack.compute.functions.ToActionResponseFunction Maven / Gradle / Ivy

/*******************************************************************************
 * 	Copyright 2016 ContainX and OpenStack4j                                          
 * 	                                                                                 
 * 	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.huawei.openstack4j.openstack.compute.functions;

import com.google.common.base.Function;

import com.huawei.openstack4j.core.transport.HttpEntityHandler;
import com.huawei.openstack4j.core.transport.HttpResponse;
import com.huawei.openstack4j.core.transport.functions.ResponseToActionResponse;
import com.huawei.openstack4j.model.common.ActionResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A Function which consumes the input of an HttpResponse and returns the
 * corresponding ActionResponse
 *
 * @author Jeremy Unruh
 */
public class ToActionResponseFunction implements Function {

    public static final ToActionResponseFunction INSTANCE = new ToActionResponseFunction();
    private static final Logger LOG = LoggerFactory.getLogger(ToActionResponseFunction.class);
    private static final String FAILED_MSG = "Cannot '%s' while instance in in state of %s";

    @Override
    public ActionResponse apply(HttpResponse response) {
        return apply(response, null);
    }

    public ActionResponse apply(HttpResponse response, String action) {
        try {
            if (response.getStatus() == 409 || response.getStatus() == 500) {
                ActionResponse ar = ResponseToActionResponse.INSTANCE.apply(response, true);
                if (ar != null) {
                    return ar;
                }

                LOG.error("{}, {}", response.getStatus(), response.getStatusMessage());
                if (action == null) {
                    return ActionResponse.actionFailed("Instance currently is in build state", 409);
                }
                return ActionResponse.actionFailed(String.format(FAILED_MSG, action, action), 409);
            }
            if (response.getStatus() >= 400 && response.getStatus() < 409) {
                return ResponseToActionResponse.INSTANCE.apply(response);
            }
            return ActionResponse.actionSuccess();
        } finally {
            HttpEntityHandler.closeQuietly(response);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy