<commit_before>package com.sequenceiq.cloudbreak.shell.commands.common;

import java.util.List;

import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;

import com.sequenceiq.cloudbreak.api.model.FlexSubscriptionRequest;
import com.sequenceiq.cloudbreak.api.model.FlexSubscriptionResponse;
import com.sequenceiq.cloudbreak.shell.model.ShellContext;

public class FlexSubscriptionCommands implements CommandMarker {

    private ShellContext shellContext;

    public FlexSubscriptionCommands(ShellContext shellContext) {
        this.shellContext = shellContext;
    }

    private boolean isFlexAvailable() {
        try {
            return shellContext.getSmartSenseSubscription() != null && !shellContext.getSmartSenseSubscription().isAutoGenerated();
        } catch (Exception ex) {
            return false;
        }
    }

    @CliAvailabilityIndicator(value = { "flex register" })
    public boolean registerAvailable() {
        return isFlexAvailable();
    }

    @CliCommand(value = "flex register", help = "Register a Flex subscription")
    public String register(
            @CliOption(key = "name", mandatory = true) String name,
            @CliOption(key = "subscriptionId", mandatory = true) String subscriptionId,
            @CliOption(key = "publicInAccount", help = "Flags if the blueprint is public in the account",
                    unspecifiedDefaultValue = "false", specifiedDefaultValue = "true") boolean publicInAccount) {
        FlexSubscriptionRequest request = new FlexSubscriptionRequest();
        request.setName(name);
        request.setSubscriptionId(subscriptionId);
        request.setSmartSenseSubscriptionId(shellContext.getSmartSenseSubscription().getId());
        try {
            Long id;
            if (publicInAccount) {
                id = shellContext.cloudbreakClient().flexSubscriptionEndpoint().postPublic(request).getId();
            } else {
                id = shellContext.cloudbreakClient().flexSubscriptionEndpoint().postPrivate(request).getId();
            }
            return String.format("Flex subscription registered with id: '%d'", id);
        } catch (Exception e) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(e);
        }
    }

    @CliAvailabilityIndicator(value = "flex set-default")
    public boolean setDefaultAvailable() {
        return isFlexAvailable();
    }

    @CliCommand(value = "flex set-default", help = "Sets the default Flex subscription")
    public String setDefault(@CliOption(key = "name", mandatory = true) String name) {
        try {
            shellContext.cloudbreakClient().flexSubscriptionEndpoint().setDefaultInAccount(name);
            return String.format("Default Flex subscription changed'", name);
        } catch (Exception e) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(e);
        }
    }

    @CliAvailabilityIndicator(value = "flex use-for-controller")
    public boolean useForControllerAvailable() {
        return isFlexAvailable();
    }

    @CliCommand(value = "flex use-for-controller", help = "Sets the Flex subscription for controller node")
    public String useForController(@CliOption(key = "name", mandatory = true) String name) {
        try {
            shellContext.cloudbreakClient().flexSubscriptionEndpoint().setUsedForControllerInAccount(name);
            return String.format("Flex subscription for controller changed'", name);
        } catch (Exception e) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(e);
        }
    }

    @CliAvailabilityIndicator(value = "flex list")
    public boolean listAvailable() {
        return isFlexAvailable();
    }

    @CliCommand(value = "flex list", help = "Shows the currently available Flex subscriptions")
    public String list() throws Exception {
        try {
            List<FlexSubscriptionResponse> publics = shellContext.cloudbreakClient().flexSubscriptionEndpoint().getPublics();
            return shellContext.outputTransformer().render(shellContext.responseTransformer().transformToMap(publics, "id", "name"), "ID", "INFO");
        } catch (Exception ex) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(ex);
        }
    }

    @CliAvailabilityIndicator(value = { "flex delete --id", "flex delete --name" })
    public boolean deleteAvailable() {
        return isFlexAvailable();
    }

    private String delete(Long id, String name) {
        try {
            if (id != null) {
                shellContext.cloudbreakClient().flexSubscriptionEndpoint().delete(id);
                return String.format("Flex subscription deleted with id: %s", id);
            } else if (name != null) {
                shellContext.cloudbreakClient().flexSubscriptionEndpoint().deletePrivate(name);
                return String.format("Flex subscription deleted with name: %s", name);
            }
            throw shellContext.exceptionTransformer().transformToRuntimeException("Id or subscription id not specified");
        } catch (Exception ex) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(ex);
        }
    }

    @CliCommand(value = "flex delete --id", help = "Deletes Flex subscription by its id")
    public String deleteById(@CliOption(key = "", mandatory = true) Long id) throws Exception {
        return delete(id, null);
    }

    @CliCommand(value = "flex delete --name", help = "Deletes Flex subscription by its name")
    public String deleteBySubscriptionId(@CliOption(key = "", mandatory = true) String name) throws Exception {
        return delete(null, name);
    }
}<commit_msg>CLOUD-78616 Protect only flex regsiter shell command<commit_after>package com.sequenceiq.cloudbreak.shell.commands.common;

import java.util.List;

import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;

import com.sequenceiq.cloudbreak.api.model.FlexSubscriptionRequest;
import com.sequenceiq.cloudbreak.api.model.FlexSubscriptionResponse;
import com.sequenceiq.cloudbreak.shell.model.ShellContext;

public class FlexSubscriptionCommands implements CommandMarker {

    private ShellContext shellContext;

    public FlexSubscriptionCommands(ShellContext shellContext) {
        this.shellContext = shellContext;
    }

    @CliAvailabilityIndicator(value = { "flex register" })
    public boolean registerAvailable() {
        try {
            return shellContext.getSmartSenseSubscription() != null && !shellContext.getSmartSenseSubscription().isAutoGenerated();
        } catch (Exception ex) {
            return false;
        }
    }

    @CliCommand(value = "flex register", help = "Register a Flex subscription")
    public String register(
            @CliOption(key = "name", mandatory = true) String name,
            @CliOption(key = "subscriptionId", mandatory = true) String subscriptionId,
            @CliOption(key = "publicInAccount", help = "Flags if the blueprint is public in the account",
                    unspecifiedDefaultValue = "false", specifiedDefaultValue = "true") boolean publicInAccount) {
        FlexSubscriptionRequest request = new FlexSubscriptionRequest();
        request.setName(name);
        request.setSubscriptionId(subscriptionId);
        request.setSmartSenseSubscriptionId(shellContext.getSmartSenseSubscription().getId());
        try {
            Long id;
            if (publicInAccount) {
                id = shellContext.cloudbreakClient().flexSubscriptionEndpoint().postPublic(request).getId();
            } else {
                id = shellContext.cloudbreakClient().flexSubscriptionEndpoint().postPrivate(request).getId();
            }
            return String.format("Flex subscription registered with id: '%d'", id);
        } catch (Exception e) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(e);
        }
    }

    @CliAvailabilityIndicator(value = "flex set-default")
    public boolean setDefaultAvailable() {
        return true;
    }

    @CliCommand(value = "flex set-default", help = "Sets the default Flex subscription")
    public String setDefault(@CliOption(key = "name", mandatory = true) String name) {
        try {
            shellContext.cloudbreakClient().flexSubscriptionEndpoint().setDefaultInAccount(name);
            return String.format("Default Flex subscription changed'", name);
        } catch (Exception e) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(e);
        }
    }

    @CliAvailabilityIndicator(value = "flex use-for-controller")
    public boolean useForControllerAvailable() {
        return true;
    }

    @CliCommand(value = "flex use-for-controller", help = "Sets the Flex subscription for controller node")
    public String useForController(@CliOption(key = "name", mandatory = true) String name) {
        try {
            shellContext.cloudbreakClient().flexSubscriptionEndpoint().setUsedForControllerInAccount(name);
            return String.format("Flex subscription for controller changed'", name);
        } catch (Exception e) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(e);
        }
    }

    @CliAvailabilityIndicator(value = "flex list")
    public boolean listAvailable() {
        return true;
    }

    @CliCommand(value = "flex list", help = "Shows the currently available Flex subscriptions")
    public String list() throws Exception {
        try {
            List<FlexSubscriptionResponse> publics = shellContext.cloudbreakClient().flexSubscriptionEndpoint().getPublics();
            return shellContext.outputTransformer().render(shellContext.responseTransformer().transformToMap(publics, "id", "name"), "ID", "INFO");
        } catch (Exception ex) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(ex);
        }
    }

    @CliAvailabilityIndicator(value = { "flex delete --id", "flex delete --name" })
    public boolean deleteAvailable() {
        return true;
    }

    private String delete(Long id, String name) {
        try {
            if (id != null) {
                shellContext.cloudbreakClient().flexSubscriptionEndpoint().delete(id);
                return String.format("Flex subscription deleted with id: %s", id);
            } else if (name != null) {
                shellContext.cloudbreakClient().flexSubscriptionEndpoint().deletePrivate(name);
                return String.format("Flex subscription deleted with name: %s", name);
            }
            throw shellContext.exceptionTransformer().transformToRuntimeException("Id or subscription id not specified");
        } catch (Exception ex) {
            throw shellContext.exceptionTransformer().transformToRuntimeException(ex);
        }
    }

    @CliCommand(value = "flex delete --id", help = "Deletes Flex subscription by its id")
    public String deleteById(@CliOption(key = "", mandatory = true) Long id) throws Exception {
        return delete(id, null);
    }

    @CliCommand(value = "flex delete --name", help = "Deletes Flex subscription by its name")
    public String deleteBySubscriptionId(@CliOption(key = "", mandatory = true) String name) throws Exception {
        return delete(null, name);
    }
}
{'fraction_non_alphanumeric': 0.10255093305940764, 'fraction_numerical': 0.00042800890258517377, 'mean_word_length': 3.2146464646464645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1121734', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2816, 'n_words': 685}