コード例 #1
0
  @CliCommand(value = "instancegroup configure", help = "Configure instance groups")
  public String create(
      @CliOption(key = "instanceGroup", mandatory = true, help = "Name of the instanceGroup")
          InstanceGroup instanceGroup,
      @CliOption(key = "nodecount", mandatory = true, help = "Nodecount for instanceGroup")
          Integer nodeCount,
      @CliOption(
              key = "ambariServer",
              mandatory = true,
              help = "Ambari server will be installed here if true")
          boolean ambariServer,
      @CliOption(key = "templateId", help = "TemplateId of the instanceGroup")
          InstanceGroupTemplateId instanceGroupTemplateId,
      @CliOption(key = "templateName", help = "TemplateName of the instanceGroup")
          InstanceGroupTemplateName instanceGroupTemplateName,
      @CliOption(key = "securityGroupId", help = "SecurityGroupId of the instanceGroup")
          SecurityGroupId instanceGroupSecurityGroupId,
      @CliOption(key = "securityGroupName", help = "SecurityGroupName of the instanceGroup")
          SecurityGroupName instanceGroupSecurityGroupName)
      throws Exception {
    try {
      String templateId;
      if (instanceGroupTemplateId != null) {
        templateId = instanceGroupTemplateId.getName();
      } else if (instanceGroupTemplateName != null) {
        TemplateResponse aPublic =
            shellContext
                .cloudbreakClient()
                .templateEndpoint()
                .getPublic(instanceGroupTemplateName.getName());
        if (aPublic != null) {
          templateId = aPublic.getId().toString();
        } else {
          return String.format(
              "Template not found by name: %s", instanceGroupTemplateName.getName());
        }
      } else {
        return "Template name or id is not defined for instanceGroup (use --templateName or --templateId)";
      }
      String securityGroupId;
      if (instanceGroupSecurityGroupId != null) {
        securityGroupId = instanceGroupSecurityGroupId.getName();
      } else if (instanceGroupSecurityGroupName != null) {
        SecurityGroupResponse aPublic =
            shellContext
                .cloudbreakClient()
                .securityGroupEndpoint()
                .getPublic(instanceGroupSecurityGroupName.getName());
        if (aPublic != null) {
          securityGroupId = aPublic.getId().toString();
        } else {
          return String.format(
              "SecurityGroup not found by name: %s", instanceGroupSecurityGroupName.getName());
        }
      } else {
        return "SecurityGroup name or id is not defined for instanceGroup (use --securityGroupName or --securityGroupId)";
      }

      Long parsedTemplateId = Longs.tryParse(templateId);
      Long parsedsecurityGroupId = Longs.tryParse(securityGroupId);
      if (parsedTemplateId != null && parsedsecurityGroupId != null) {
        if (ambariServer) {
          boolean ambariSpecified =
              shellContext
                  .getInstanceGroups()
                  .values()
                  .stream()
                  .filter(e -> e.getType().equals("GATEWAY"))
                  .findAny()
                  .isPresent();
          if (ambariSpecified) {
            return "Ambari server is already specified";
          }
          if (nodeCount != 1) {
            return "Allowed node count for Ambari server: 1";
          }
          shellContext.putInstanceGroup(
              instanceGroup.getName(),
              new InstanceGroupEntry(
                  parsedTemplateId, parsedsecurityGroupId, nodeCount, "GATEWAY"));
        } else {
          shellContext.putInstanceGroup(
              instanceGroup.getName(),
              new InstanceGroupEntry(parsedTemplateId, parsedsecurityGroupId, nodeCount, "CORE"));
        }
        shellContext.putHostGroup(
            instanceGroup.getName(),
            new HostgroupEntry(nodeCount, new HashSet<>(), RecoveryMode.MANUAL));
        if (shellContext.getActiveHostGroups().size() == shellContext.getInstanceGroups().size()
            && shellContext.getActiveHostGroups().size() != 0) {
          shellContext.setHint(Hints.SELECT_NETWORK);
        } else {
          shellContext.setHint(Hints.CONFIGURE_HOSTGROUP);
        }
        return shellContext
            .outputTransformer()
            .render(shellContext.getInstanceGroups(), "instanceGroup");
      } else {
        return "TemplateId is not a number.";
      }
    } catch (Exception ex) {
      throw shellContext.exceptionTransformer().transformToRuntimeException(ex);
    }
  }