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

com.infusers.core.version.deployment.CloudProviderService Maven / Gradle / Ivy

There is a newer version: 2025.01.0002
Show newest version
package com.infusers.core.version.deployment;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

//import com.infusers.core.audit.AuditService;

@Service
public class CloudProviderService {

    private final Environment environment;
    
	private static final String CLASS_NAME = "CloudProviderService";

	//@Autowired(required = true)
	//private AuditService auditService;
	
	final Logger log = LogManager.getLogger(CloudProviderService.class);    

    @Autowired
    public CloudProviderService(Environment environment) {
        this.environment = environment;
    }

    public ResponseEntity getCloudProvider() {
		//auditService.logAudit("", "CloudProviderService", "", "getCloudProvider", "Non protected API");    	
		log.debug("CloudProviderService.getCloudProvider() Fetching deployment host detail.");

		StringBuffer cloudProviderInfo = new StringBuffer();
		cloudProviderInfo.append(getCloudProviderName());
		
	    // Create success response
	    Map response = new HashMap<>();
	    response.put("status", "success");
	    response.put("data", cloudProviderInfo);

	    // Return response with 200 status code
	    return new ResponseEntity<>(response, HttpStatus.OK);
    }
    
    public String getCloudProviderName() {
		//auditService.logAudit("", "CloudProviderService", "", "getCloudProviderName", "Non protected API");    	
		log.debug("CloudProviderService.getCloudProviderName() Fetching deployment host detail.");    	
    	
		String cloudName = "Unknown Cloud Provider/Local Laptop";
		
        String[] activeProfiles = environment.getActiveProfiles();

        for (String profile : activeProfiles) {
            if ("aws".equalsIgnoreCase(profile)) {
            	cloudName =  "Amazon Web Services (AWS)";
            	break;
            } 
            else if ("gcp".equalsIgnoreCase(profile)) {
            	cloudName =  "Google Cloud Platform (GCP)";
            	break;
            } 
            else if ("az".equalsIgnoreCase(profile)) {
            	cloudName =  "Microsoft Azure";
            	break;
            }
            // Add checks for other profiles here
        }
        //return "Hosted on: "+cloudName;
        return cloudName;	        
    }    
}