Example #1
0
    def _ConstructCreateSettingsFromArgs(cls,
                                         sql_messages,
                                         args,
                                         instance=None,
                                         release_track=DEFAULT_RELEASE_TRACK):
        """Constructs patch settings object from base settings and args."""
        original_settings = instance.settings if instance else None
        settings = cls._ConstructBaseSettingsFromArgs(sql_messages, args,
                                                      instance, release_track)

        if args.on_premises_host_port:
            if args.require_ssl:
                raise exceptions.ToolException(
                    'Argument --on-premises-host-port not '
                    'allowed with --require_ssl')
            settings.onPremisesConfiguration = sql_messages.OnPremisesConfiguration(
                hostPort=args.on_premises_host_port)

        backup_configuration = (reducers.BackupConfiguration(
            sql_messages,
            instance,
            backup=args.backup,
            backup_start_time=args.backup_start_time,
            enable_bin_log=args.enable_bin_log))
        if backup_configuration:
            cls.AddBackupConfigToSettings(settings, backup_configuration)

        settings.databaseFlags = (reducers.DatabaseFlags(
            sql_messages,
            original_settings,
            database_flags=args.database_flags))

        settings.maintenanceWindow = (reducers.MaintenanceWindow(
            sql_messages,
            instance,
            maintenance_release_channel=args.maintenance_release_channel,
            maintenance_window_day=args.maintenance_window_day,
            maintenance_window_hour=args.maintenance_window_hour))

        if args.storage_type:
            settings.dataDiskType = STORAGE_TYPE_PREFIX + args.storage_type

        # BETA args.
        if release_track == base.ReleaseTrack.BETA:
            labels = reducers.UserLabels(sql_messages,
                                         instance,
                                         labels=args.labels)
            if labels:
                settings.userLabels = labels

            # Check that availability type is only specified if this is Postgres.
            if (args.IsSpecified('availability_type') and
                    not api_util.InstancesV1Beta4.IsPostgresDatabaseVersion(
                        args.database_version)):
                raise exceptions.InvalidArgumentException(
                    '--availability-type',
                    'Cannot set [--availability-type] on a '
                    'non-Postgres instance.')

        return settings
Example #2
0
    def _ConstructCreateSettingsFromArgs(cls,
                                         sql_messages,
                                         args,
                                         instance=None,
                                         release_track=DEFAULT_RELEASE_TRACK):
        """Constructs patch settings object from base settings and args."""
        original_settings = instance.settings if instance else None
        settings = cls._ConstructBaseSettingsFromArgs(sql_messages, args,
                                                      instance, release_track)

        if args.on_premises_host_port:
            if args.require_ssl:
                raise exceptions.ToolException(
                    'Argument --on-premises-host-port not '
                    'allowed with --require_ssl')
            settings.onPremisesConfiguration = sql_messages.OnPremisesConfiguration(
                hostPort=args.on_premises_host_port)

        backup_configuration = (reducers.BackupConfiguration(
            sql_messages,
            instance,
            backup=args.backup,
            backup_start_time=args.backup_start_time,
            enable_bin_log=args.enable_bin_log))
        if backup_configuration:
            cls.AddBackupConfigToSettings(settings, backup_configuration)

        settings.databaseFlags = (reducers.DatabaseFlags(
            sql_messages,
            original_settings,
            database_flags=args.database_flags))

        settings.maintenanceWindow = (reducers.MaintenanceWindow(
            sql_messages,
            instance,
            maintenance_release_channel=args.maintenance_release_channel,
            maintenance_window_day=args.maintenance_window_day,
            maintenance_window_hour=args.maintenance_window_hour))

        if args.storage_type:
            settings.dataDiskType = STORAGE_TYPE_PREFIX + args.storage_type

        # BETA args.
        if release_track == base.ReleaseTrack.BETA:
            labels = reducers.UserLabels(sql_messages,
                                         instance,
                                         labels=args.labels)
            if labels:
                settings.userLabels = labels

        return settings
Example #3
0
    def _ConstructPatchSettingsFromArgs(cls,
                                        sql_messages,
                                        args,
                                        instance=None,
                                        release_track=DEFAULT_RELEASE_TRACK):
        """Constructs create settings object from base settings and args."""
        original_settings = instance.settings if instance else None
        settings = cls._ConstructBaseSettingsFromArgs(sql_messages, args,
                                                      instance, release_track)

        if args.clear_gae_apps:
            settings.authorizedGaeApplications = []

        if any([args.follow_gae_app, args.gce_zone]):
            settings.locationPreference = sql_messages.LocationPreference(
                followGaeApplication=args.follow_gae_app, zone=args.gce_zone)

        if args.clear_authorized_networks:
            if not settings.ipConfiguration:
                settings.ipConfiguration = sql_messages.IpConfiguration()
            settings.ipConfiguration.authorizedNetworks = []

        if args.enable_database_replication is not None:
            settings.databaseReplicationEnabled = args.enable_database_replication

        backup_configuration = (reducers.BackupConfiguration(
            sql_messages,
            instance,
            no_backup=args.no_backup,
            backup_start_time=args.backup_start_time,
            enable_bin_log=args.enable_bin_log))
        if backup_configuration:
            cls.AddBackupConfigToSettings(settings, backup_configuration)

        settings.databaseFlags = (reducers.DatabaseFlags(
            sql_messages,
            original_settings,
            database_flags=args.database_flags,
            clear_database_flags=args.clear_database_flags))

        settings.maintenanceWindow = (reducers.MaintenanceWindow(
            sql_messages,
            instance,
            maintenance_release_channel=args.maintenance_release_channel,
            maintenance_window_day=args.maintenance_window_day,
            maintenance_window_hour=args.maintenance_window_hour))

        # BETA args.
        if release_track == base.ReleaseTrack.BETA:
            update_labels, remove_labels = {}, []
            if args.update_labels or args.remove_labels:
                update_labels, remove_labels = labels_util.GetAndValidateOpsFromArgs(
                    args)
            labels = reducers.UserLabels(sql_messages,
                                         instance,
                                         update_labels=update_labels,
                                         remove_labels=remove_labels,
                                         clear_labels=args.clear_labels)
            if labels:
                settings.userLabels = labels

        return settings
Example #4
0
    def _ConstructSettingsFromArgs(cls, sql_messages, args, instance=None):
        """Constructs instance settings from the command line arguments.

    Args:
      sql_messages: module, The messages module that should be used.
      args: argparse.Namespace, The arguments that this command was invoked
          with.
      instance: sql_messages.DatabaseInstance, The original instance, for
          settings that depend on the previous state.

    Returns:
      A settings object representing the instance settings.

    Raises:
      ToolException: An error other than http error occured while executing the
          command.
    """
        settings = sql_messages.Settings(
            tier=reducers.MachineType(instance, args.tier, args.memory,
                                      args.cpu),
            pricingPlan=args.pricing_plan,
            replicationType=args.replication,
            activationPolicy=args.activation_policy)

        labels = None
        if hasattr(args, 'labels'):
            labels = reducers.UserLabels(sql_messages,
                                         instance,
                                         labels=args.labels)
        elif (hasattr(args, 'update_labels') and args.update_labels
              or hasattr(args, 'remove_labels') and args.remove_labels):
            update_labels, remove_labels = labels_util.GetAndValidateOpsFromArgs(
                args)
            labels = reducers.UserLabels(sql_messages,
                                         instance,
                                         update_labels=update_labels,
                                         remove_labels=remove_labels)
        elif hasattr(args, 'clear_labels'):
            labels = reducers.UserLabels(sql_messages,
                                         instance,
                                         clear_labels=args.clear_labels)
        if labels:
            settings.userLabels = labels

        # these args are only present for the patch command
        clear_authorized_networks = getattr(args, 'clear_authorized_networks',
                                            False)
        clear_gae_apps = getattr(args, 'clear_gae_apps', False)

        if args.authorized_gae_apps:
            settings.authorizedGaeApplications = args.authorized_gae_apps
        elif clear_gae_apps:
            settings.authorizedGaeApplications = []

        if any([
                args.assign_ip is not None, args.require_ssl is not None,
                args.authorized_networks, clear_authorized_networks
        ]):
            settings.ipConfiguration = sql_messages.IpConfiguration()
            if args.assign_ip is not None:
                if hasattr(settings.ipConfiguration, 'enabled'):
                    # v1beta3 is being used; use 'enabled' instead of 'ipv4Enabled'.
                    settings.ipConfiguration.enabled = args.assign_ip
                else:
                    settings.ipConfiguration.ipv4Enabled = args.assign_ip

            if args.authorized_networks:
                # AclEntry is only available in the v1beta4 version of the API. If it is
                # present, the API expects an AclEntry for the authorizedNetworks list;
                # otherwise, it expects a string.
                if getattr(sql_messages, 'AclEntry', None) is not None:
                    authorized_networks = [
                        sql_messages.AclEntry(value=n)
                        for n in args.authorized_networks
                    ]
                else:
                    authorized_networks = args.authorized_networks
                settings.ipConfiguration.authorizedNetworks = authorized_networks
            if clear_authorized_networks:
                # For patch requests, this field needs to be labeled explicitly cleared.
                settings.ipConfiguration.authorizedNetworks = []

            if args.require_ssl is not None:
                settings.ipConfiguration.requireSsl = args.require_ssl

        if any([args.follow_gae_app, args.gce_zone]):
            settings.locationPreference = sql_messages.LocationPreference(
                followGaeApplication=args.follow_gae_app, zone=args.gce_zone)

        if getattr(args, 'enable_database_replication', None) is not None:
            settings.databaseReplicationEnabled = args.enable_database_replication

        return settings