Ejemplo n.º 1
0
def test_chart_with_empties(value):
    with pytest.raises(ValueError):
        Chart().with_name(value)
        Chart().with_description(value)
        Chart().with_program()
        FieldOption(value, False)
        PublishLabelOptions(value, value, value, value, value)
Ejemplo n.º 2
0
    def delete(self, resource_id=None, dry_run=False):
        """Deletes a SignalFx dashboard using the /dashboard/<id> helper
        endpoint. Dashboard Id is required

        Arguments:
            resource_id: String matching the resource id for a dashboard
            dry_run: Boolean to test a dry run

        See: https://developers.signalfx.com/dashboards_reference.html#tag/Delete-Single-Dashboard
        """
        if dry_run:
            click.echo(
                "Dashboard id \"{0}\" will be deleted. API call that is executed: \n DELETE {1}"
                .format(self.options['id'], (self.base_url + self.endpoint +
                                             '/' + self.options['id'])))
            return None
        """So, as per SignalFx design as of 02/19/18,
        Deleting a dashboard via the API does not affect the related charts in any way.
        Only the relationship between the charts and the dashboard is severed. Any charts on the deleted dashboard are
        orphaned and available to other dashboards. If you wish to delete them at the same time you delete their
        current dashboard you must send a Delete Chart API call for each chart. That's exactly what we are doing here.
        """
        list_of_charts = [
            charts['chartId']
            for charts in self.read(resource_id=resource_id)['charts']
        ]
        if list_of_charts:
            for chart in list_of_charts:
                Chart(session=self.session_handler).with_api_token(
                    self.api_token).with_id(chart).delete()

        return super(Dashboard, self).delete(resource_id=resource_id)
Ejemplo n.º 3
0
 def get_config_helper(id):
     res = Chart(session=self.session_handler).with_api_token(
         self.api_token).with_id(id).read()
     return {'id': id, 'name': res['name']}
Ejemplo n.º 4
0
    def __update_child_resources__(self, chart_state):
        """Update child resources for this dashboard.
        """
        state = deepcopy(chart_state)

        # Dashboard state can get really screwy sometimes. In certain
        # situations a stale Chart object can be in the Dashboard config
        # _without_ a valid id. Let's be nice and clean that up; makes it
        # easier for us to update Dashboards too.
        for chart in state:
            if chart['chartId'] is None:
                state.remove(chart)

        remote_chart_ids = list(map(lambda x: x['chartId'], state))

        def get_config_helper(id):
            res = Chart(session=self.session_handler).with_api_token(
                self.api_token).with_id(id).read()
            return {'id': id, 'name': res['name']}

        remote_charts = list(map(get_config_helper, remote_chart_ids))

        local_charts = self.__get__('charts', [])

        # Update charts that exist in SignalFx
        for remote_chart in remote_charts:
            for local_chart in local_charts:
                if remote_chart['name'] == local_chart.__get__('name'):
                    local_chart.with_id(remote_chart['id']).with_api_token(
                        self.api_token).update()
                    break

        # Delete charts that exist in SignalFx but not in our local config
        local_names = list(map(lambda x: x.__get__('name'), local_charts))
        for remote_chart in remote_charts:
            if remote_chart['name'] not in local_names:
                Chart(session=self.session_handler).with_id(
                    remote_chart['id']).with_api_token(
                        self.api_token).delete()
                # Deleting the chart from state to make sure empty chart states are not returned back to the update
                # call which has adverse effects(throws a 500 error)
                state[:] = [
                    d for d in state if d.get('chartId') != remote_chart['id']
                ]

        # Create charts that exist in our local config but not in SignalFx
        remote_names = list(map(lambda x: x['name'], remote_charts))
        for local_chart in local_charts:
            if local_chart.__get__('name') not in remote_names:
                resp = local_chart.with_api_token(self.api_token).create()

                # We need different row and column values when we are creating new charts so that they won't overlap
                # with one another
                row_nums = [chart['row'] for chart in state]
                column_nums = [chart['column'] for chart in state]
                row_num_to_set = 0
                column_num_to_set = 0
                if 0 in row_nums:
                    row_num_to_set = max(set(row_nums)) + 1
                if 0 in column_nums:
                    column_num_to_set = max(set(column_nums)) + 1
                state.append({
                    'chartId': resp['id'],
                    'column': column_num_to_set,
                    'height': 2,
                    'row': row_num_to_set,
                    'width': 6
                })
        return state
def test_chart_with_program():
    expected = Data('Ms. Communication')
    chart = Chart().with_program(expected)
    assert chart.options['programText'] == expected
def test_chart_with_description():
    expected = 'Petite frere'
    chart = Chart().with_description(expected)
    assert chart.options['description'] == expected
def test_chart_with_name():
    expected = 'El Aparecido'
    chart = Chart().with_name(expected)
    assert chart.options['name'] == expected
def test_chart_init():
    chart = Chart()
    assert chart.endpoint == '/chart'
    assert chart.options == {}
    expected = 'Petite frere'
    chart = Chart().with_description(expected)
    assert chart.options['description'] == expected


def test_chart_with_program():
    expected = Data('Ms. Communication')
    chart = Chart().with_program(expected)
    assert chart.options['programText'] == expected


def test_ts_chart_init():
    assert TimeSeriesChart().chart_options['type'] == 'TimeSeriesChart'


@pytest.mark.parametrize("bad_input", [None, "", Chart()])
def test_ts_chart_without_enums(bad_input):
    with pytest.raises(ValueError):
        TimeSeriesChart().with_unit_prefix(bad_input)
        TimeSeriesChart().with_color_by(bad_input)
        TimeSeriesChart().with_default_plot_type(bad_input)


@pytest.mark.parametrize("prefix", UnitPrefix)
def test_ts_chart_with_prefix(prefix):
    chart = TimeSeriesChart().with_unit_prefix(prefix)
    assert chart.chart_options['unitPrefix'] == prefix.value


@pytest.mark.parametrize("color_by", ColorBy)
def test_ts_chart_with_color_by(color_by):