Untitled

mail@pastecode.io avatar
unknown
java
6 months ago
3.2 kB
1
Indexable
Never
import java.util.ArrayList;
import java.util.List;

// Abstract base class for cloud resources
abstract class CloudResource {
    protected String resourceID;
    protected String resourceType;
    protected String[] tags;
    protected String creationTime;

    public CloudResource(String resourceID, String resourceType) {
        this.resourceID = resourceID;
        this.resourceType = resourceType;
    }

    // Default deploy behavior
    public void deploy() {
        System.out.println("Deploying " + resourceType);
    }

    // Default terminate behavior
    public void terminate() {
        System.out.println("Terminating " + resourceType);
    }

    public void updateConfiguration() {
        // Default config update
    }
}

// EC2 instance resource
class EC2Instance extends CloudResource {
    private String instanceType;

    public EC2Instance(String resourceID, String instanceType) {
        super(resourceID, "EC2");
        this.instanceType = instanceType;
    }

    // Overridden deploy for EC2
    @Override
    public void deploy() {
        System.out.println("Launching EC2 instance");
    }

    // Overridden terminate for EC2
    @Override
    public void terminate() {
        System.out.println("Shutting down EC2 instance");
    }
}

// S3 Bucket resource
class S3Bucket extends CloudResource {
    private String bucketName;

    public S3Bucket(String resourceID, String bucketName) {
        super(resourceID, "S3");
        this.bucketName = bucketName;
    }

    // Overridden deploy for S3
    @Override
    public void deploy() {
        System.out.println("Creating S3 bucket");
    }

    // Overridden terminate for S3
    @Override
    public void terminate() {
        System.out.println("Deleting S3 bucket");
    }
}

// Manager for cloud deployments
class DeploymentManager {
    private List<CloudResource> resourcesList;

    public DeploymentManager() {
        resourcesList = new ArrayList<>();
    }

    // Add a cloud resource
    public void addResource(CloudResource resource) {
        resourcesList.add(resource);
    }

    // Remove a cloud resource
    public void removeResource(CloudResource resource) {
        resourcesList.remove(resource);
    }

    // Deploy all resources
    public void deployAllResources() {
        for (CloudResource resource : resourcesList) {
            resource.deploy();
        }
    }

    // Terminate all resources
    public void terminateAllResources() {
        for (CloudResource resource : resourcesList) {
            resource.terminate();
        }
    }
}

// Entry point for the system
public class CloudDeploymentSystem {
    public static void main(String[] args) {
        EC2Instance instance = new EC2Instance("i-12345", "t2.micro");
        S3Bucket bucket = new S3Bucket("s3-67890", "myBucket");

        DeploymentManager manager = new DeploymentManager();
        manager.addResource(instance);
        manager.addResource(bucket);

        manager.deployAllResources();
        manager.terminateAllResources();
    }
}