def create(self, dry_run=False, force=False, interactive=False): """Creates a Signalfx dashboard using the /dashboard/simple helper endpoint. A list of chart models is required. See: https://developers.signalfx.com/v2/reference#dashboardsimple """ if dry_run: self.options.update({'charts': util.flatten_charts(self.options)}) click.echo( "Creates a new Dashboard named: \"{0}\". API call being executed: \n" "POST {1} \nRequest Body: \n {2}".format( self.options['name'], (self.base_url + self.endpoint + '/simple?name=' + self.__get__('name')), self.options)) return None if self.__create_helper__(force=force, interactive=interactive): dashboard_create_response = self.__action__( 'post', self.endpoint + '/simple', lambda x: util.flatten_charts(self.options), params={'name': self.__get__('name')}, dry_run=dry_run, interactive=interactive, force=force) """Check to see if there are any filters defined, If so, update the dashboard with those filters. NOTE: This cannot be done during dashboard creation as we are using /simple endpoint which doesn't support passing filters """ if bool(self.filters['filters']): dashboard_create_response['filters'] = self.filters['filters'] self.options = dashboard_create_response return super(Dashboard, self).update() else: return dashboard_create_response
def __action__(self, action, endpoint, update_fn, params=None, dry_run=False, interactive=False, force=False): """Perform a stateful HTTP action against the SignalFx API. Arguments: action: the action to take update_fn: callback allowing modification of the payload before sending to SignalFx. dry_run: When true, this resource prints its configured state without calling SignalFx. interactive: When true, this resource asks the caller which version resource to modify. force: When true, this resource modifies itself in SignalFx disregarding previous SignalFx state. Returns: The JSON response if successful. Raises: HTTPError: when a network exception occurred """ if dry_run: opts = dict(self.options) if self.options.get('charts', []): opts.update({'charts': util.flatten_charts(self.options)}) return opts util.assert_valid(self.api_token) response = self.session_handler.request(action, url=self.base_url + endpoint, params=params, json=update_fn(self.options), headers={ 'X-SF-Token': self.api_token, 'Content-Type': 'application/json' }) debug("{0} {1}".format(response.request.method.upper(), response.request.url)) if response.request.body: debug(response.request.body) try: response.raise_for_status() return response.json() except requests.exceptions.HTTPError as error: if response.status_code == 405: msg = "Oops! We encountered a 405 exception for {0}" +\ " resource. Possible cause: You might be passing an " +\ "empty id" click.echo(msg.format(str(self.__class__.__name__))) sys.exit(1) # Tell the user exactly what went wrong according to SignalFX raise RuntimeError(error.response.text) except JSONDecodeError: # Some responses from the API don't return anything, in these # situations we shouldn't either return None
def create(self, group_id=None, dry_run=False, force=False, interactive=False): """Creates a Signalfx dashboard using the /dashboard/simple helper endpoint. A list of chart models is required. Arguments: group_id: String Group Id of the Dashboard Group you want to create this dashboard in dry_run: Boolean to test a dry run force: Boolean to force creation in case of errors interactive: Boolean to be prompted for user input in case of errors See: https://developers.signalfx.com/dashboards_reference.html#tag/Create-Simple-Dashboard """ if dry_run: self.options.update({'charts': util.flatten_charts(self.options)}) click.echo( "Creates a new Dashboard named: \"{0}\". API call being executed: \n" "POST {1} \nRequest Body: \n {2}".format( self.options['name'], (self.base_url + self.endpoint + '/simple?name=' + self.__get__('name') + ('&groupId=' + group_id if group_id else '')), self.options)) return None if self.__create_helper__(force=force, interactive=interactive): dashboard_create_response = self.__action__( 'post', self.endpoint + '/simple', lambda x: util.flatten_charts(self.options), params={ 'name': self.__get__('name'), 'groupId': group_id }, dry_run=dry_run, interactive=interactive, force=force) """Check to see if there are any filters, event overlays, or "selected" event overlays defined, If so, update the dashboard with those filters and/or event overlays. NOTE: This cannot be done for filters during dashboard creation as we are using /simple endpoint which doesn't support passing filters """ if self.events['eventOverlays'] != list( ) and self.selectedevents['selectedEventOverlays'] != list(): dashboard_create_response['eventOverlays'] = list( map(lambda e: e.to_dict(), self.events['eventOverlays'])) dashboard_create_response['selectedEventOverlays'] = list( map(lambda e: e.to_dict(), self.selectedevents['selectedEventOverlays'])) self.options = dashboard_create_response if self.events['eventOverlays'] != list(): dashboard_create_response['eventOverlays'] = list( map(lambda e: e.to_dict(), self.events['eventOverlays'])) self.options = dashboard_create_response return super(Dashboard, self).update() if self.selectedevents['selectedEventOverlays'] != list(): dashboard_create_response['selectedEventOverlays'] = list( map(lambda e: e.to_dict(), self.selectedevents['selectedEventOverlays'])) self.options = dashboard_create_response return super(Dashboard, self).update() if bool(self.filters['filters']): dashboard_create_response['filters'] = self.filters['filters'] self.options = dashboard_create_response return super(Dashboard, self).update() else: return dashboard_create_response