-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Add client for SystemLink jobs API #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 11 commits
c2278f1
cbba5c7
258f373
4e22c28
6070c71
55684d8
412c8ef
2f91a6c
63083f5
5a4f0ae
1e17b89
13accda
5dd2381
4a4b977
814f1fb
85cc103
3ef21ec
6167da2
0e33a87
5575f20
ee1d908
3e727c8
6ebb8f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| .. _api_tag_page: | ||
|
|
||
| nisystemlink.clients.system | ||
| ====================== | ||
|
|
||
| .. autoclass:: nisystemlink.clients.system.SystemClient | ||
| :exclude-members: __init__ | ||
|
|
||
| .. automethod:: __init__ | ||
| .. automethod:: list_jobs | ||
| .. automethod:: create_job | ||
| .. automethod:: get_job_summary | ||
| .. automethod:: query_jobs | ||
| .. automethod:: cancel_jobs | ||
|
|
||
| .. automodule:: nisystemlink.clients.system.models | ||
| :members: | ||
| :imported-members: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| from nisystemlink.clients.core import HttpConfiguration | ||
| from nisystemlink.clients.system import SystemClient | ||
| from nisystemlink.clients.system.models import ( | ||
| CancelJobRequest, | ||
| CreateJobRequest, | ||
| JobState, | ||
| QueryJobsRequest, | ||
| ) | ||
|
|
||
| # Setup the server configuration to point to your instance of SystemLink Enterprise | ||
| server_configuration = HttpConfiguration( | ||
| server_uri="https://yourserver.yourcompany.com", | ||
| api_key="YourAPIKeyGeneratedFromSystemLink", | ||
| ) | ||
| client = SystemClient(configuration=server_configuration) | ||
|
|
||
| # Get all jobs that have succeeded | ||
| jobs = client.list_jobs( | ||
| system_id="system_id", | ||
| jid="jid", | ||
| state=JobState.SUCCEEDED, | ||
| function="function", | ||
| skip=0, | ||
| take=10, | ||
| ) | ||
|
|
||
| # Create a job | ||
| arg = [["A description"]] | ||
| tgt = ["HVM_domU--SN-ec200972-eeca-062e-5bf5-017a25451b39--MAC-0A-E1-20-D6-96-2B"] | ||
| fun = ["system.set_computer_desc"] | ||
| metadata = {"queued": True, "refresh_minion_cache": {"grains": True}} | ||
| job = CreateJobRequest( | ||
| arg=arg, | ||
| tgt=tgt, | ||
| fun=fun, | ||
| metadata=metadata, | ||
| ) | ||
|
|
||
| create_job_response = client.create_job(job) | ||
|
|
||
| # Get job summary | ||
| job_summary = client.get_job_summary() | ||
|
|
||
| # Query jobs | ||
| query_job = QueryJobsRequest( | ||
| skip=0, | ||
| take=1000, | ||
| filter="result.success.Contains(false)", | ||
| projection="new(id,jid,state,lastUpdatedTimestamp,metadata.queued as queued)", | ||
| orderBy="createdTimestamp descending", | ||
| ) | ||
| query_jobs_response = client.query_jobs(query_job) | ||
|
|
||
|
|
||
| # Cancel a job | ||
| cancel_job_request = CancelJobRequest(jid=create_job_response.jid, tgt=tgt[0]) | ||
| cancel_job_response = client.cancel_jobs([cancel_job_request]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from ._system_client import SystemClient | ||
|
|
||
| # flake8: noqa |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,132 @@ | ||||||
| from typing import List, Optional | ||||||
|
|
||||||
| from nisystemlink.clients import core | ||||||
| from nisystemlink.clients.core._uplink._base_client import BaseClient | ||||||
| from nisystemlink.clients.core._uplink._methods import ( | ||||||
| get, | ||||||
| post, | ||||||
| ) | ||||||
| from uplink import Query | ||||||
|
|
||||||
| from . import models | ||||||
|
|
||||||
|
|
||||||
| class SystemClient(BaseClient): | ||||||
| def __init__(self, configuration: Optional[core.HttpConfiguration] = None): | ||||||
| """Initialize an instance. | ||||||
|
|
||||||
| Args: | ||||||
| configuration: Defines the web server to connect to and information about | ||||||
| how to connect. If not provided, the | ||||||
| :class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>` | ||||||
| is used to obtain the configuration. | ||||||
|
|
||||||
| Raises: | ||||||
| ApiException: if unable to communicate with the System Service. | ||||||
| """ | ||||||
| if configuration is None: | ||||||
| configuration = core.HttpConfigurationManager.get_configuration() | ||||||
|
|
||||||
| super().__init__(configuration, "/nisysmgmt/v1/") | ||||||
|
|
||||||
| @get( | ||||||
| "jobs", | ||||||
| args=[ | ||||||
| Query("systemId"), | ||||||
| Query("jid"), | ||||||
| Query("state"), | ||||||
| Query("function"), | ||||||
| Query("skip"), | ||||||
| Query("take"), | ||||||
| ], | ||||||
| ) | ||||||
| def list_jobs( | ||||||
| self, | ||||||
| system_id: Optional[str] = None, | ||||||
| jid: Optional[str] = None, | ||||||
| state: Optional[models.JobState] = None, | ||||||
| function: Optional[str] = None, | ||||||
| skip: Optional[int] = None, | ||||||
| take: Optional[int] = None, | ||||||
| ) -> List[models.Job]: | ||||||
| """List the jobs that matched the criteria. | ||||||
|
|
||||||
| Args: | ||||||
| system_id: The ID of the system to that the jobs belong. | ||||||
| jid: The ID of the job. | ||||||
| state: The state of the jobs. | ||||||
| function: The salt function to run on the client. | ||||||
| skip: The number of jobs to skip. | ||||||
| take: The number of jobs to return. | ||||||
|
|
||||||
| Returns: | ||||||
| The list of jobs that matched the criteria. | ||||||
|
|
||||||
| Raises: | ||||||
| ApiException: if unable to communicate with the System Service | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update in all other places
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated it, & reflected the same in other places. |
||||||
| or provided an invalid argument. | ||||||
| """ | ||||||
| ... | ||||||
|
|
||||||
| @post("jobs") | ||||||
| def create_job(self, job: models.CreateJobRequest) -> models.CreateJobResponse: | ||||||
| """Create a job. | ||||||
|
|
||||||
| Args: | ||||||
| job: The request to create the job. | ||||||
|
|
||||||
| Returns: | ||||||
| The job that was created. | ||||||
|
|
||||||
| Raises: | ||||||
| ApiException: if unable to communicate with the System Service | ||||||
| or provided an invalid argument. | ||||||
| """ | ||||||
| ... | ||||||
|
|
||||||
| @get("get-jobs-summary") | ||||||
| def get_job_summary(self) -> models.JobSummaryResponse: | ||||||
| """Get a summary of the jobs. | ||||||
|
|
||||||
| Returns: | ||||||
| An instance of a JobsSummaryResponse. | ||||||
|
|
||||||
| Raises: | ||||||
| ApiException: if unable to communicate with the System Service | ||||||
| or provided an invalid argument. | ||||||
| """ | ||||||
| ... | ||||||
|
|
||||||
| @post("query-jobs") | ||||||
| def query_jobs(self, query: models.QueryJobsRequest) -> models.QueryJobsResponse: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Skipping the suggestion as query jobs are not paged as it don't have continuation token property. |
||||||
| """Query the jobs. | ||||||
|
|
||||||
| Args: | ||||||
| query: The request to query the jobs. | ||||||
|
|
||||||
| Returns: | ||||||
| An instance of QueryJobsRequest. | ||||||
|
|
||||||
| Raises: | ||||||
| ApiException: if unable to communicate with the System Service | ||||||
| or provided an invalid argument. | ||||||
| """ | ||||||
| ... | ||||||
|
|
||||||
| @post("cancel-jobs") | ||||||
| def cancel_jobs( | ||||||
| self, job_ids: List[models.CancelJobRequest] | ||||||
| ) -> models.CancelJobResponse: | ||||||
| """Cancel the jobs. | ||||||
|
|
||||||
| Args: | ||||||
| job_ids: List of CancelJobRequest. | ||||||
|
|
||||||
| Returns: | ||||||
| The errors that appear while attempting the operation. | ||||||
|
|
||||||
| Raises: | ||||||
| ApiException: if unable to communicate with the System Service | ||||||
| or provided an invalid argument. | ||||||
| """ | ||||||
| ... | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from ._job_state import JobState | ||
| from ._job import Job | ||
| from ._create_job_request import CreateJobRequest | ||
| from ._create_job_response import CreateJobResponse | ||
| from ._job_summary_response import JobSummaryResponse | ||
| from ._query_jobs_request import QueryJobsRequest | ||
| from ._query_jobs_response import QueryJobsResponse | ||
| from ._cancel_job_request import CancelJobRequest | ||
| from ._cancel_job_response import CancelJobResponse | ||
|
|
||
| # flake8: noqa |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from typing import Optional | ||
|
|
||
| from nisystemlink.clients.core._uplink._json_model import JsonModel | ||
|
|
||
|
|
||
| class CancelJobRequest(JsonModel): | ||
| """Model for cancel job request.""" | ||
|
|
||
| jid: Optional[str] = None | ||
| """The ID of the job to cancel.""" | ||
|
|
||
| system_id: Optional[str] = None | ||
| """The system ID that the job to cancel targets.""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from typing import Optional | ||
|
|
||
| from nisystemlink.clients.core._uplink._json_model import JsonModel | ||
|
|
||
| from ._http_error import HttpError | ||
|
|
||
|
|
||
| class CancelJobResponse(JsonModel): | ||
| """Model for response of a cancel job request.""" | ||
|
|
||
| error: Optional[HttpError] = None | ||
| """Represents the standard error structure.""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from ._job_config import JobConfig | ||
|
|
||
|
|
||
| class CreateJobRequest(JobConfig): | ||
| """Model for create job request.""" | ||
|
|
||
| metadata: Optional[Dict[str, Any]] = None | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update it with actual properties..
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated it. As |
||
| """The metadata associated with the job.""" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from typing import Optional | ||
|
|
||
| from ._create_job_request import CreateJobRequest | ||
| from ._http_error import HttpError | ||
|
|
||
|
|
||
| class CreateJobResponse(CreateJobRequest): | ||
| """Model for response of create job request.""" | ||
|
|
||
| error: Optional[HttpError] = None | ||
| """Represents the standard error structure.""" | ||
|
|
||
| jid: Optional[str] = None | ||
| """The job ID.""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from typing import List, Optional | ||
|
|
||
| from nisystemlink.clients.core._uplink._json_model import JsonModel | ||
|
|
||
|
|
||
| class HttpError(JsonModel): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated it, & reflected the same in other places. |
||
| """Represents the standard error structure.""" | ||
|
|
||
| name: Optional[str] = None | ||
| """Gets the fully-qualified name that identifies the error code, such as the output of | ||
| M:NationalInstruments.SystemLink.ServiceBase.ErrorHandling.ErrorCode.ToString.""" | ||
|
|
||
| code: Optional[int] = None | ||
| """Gets the optional NationalInstruments.SystemLink.ServiceBase.ErrorHandling.ErrorCode.NumericCode | ||
| (not HTTP status code) that identifies the error code.""" | ||
|
|
||
| message: Optional[str] = None | ||
| """Gets the formatted error message, which is the NationalInstruments.SystemLink.ServiceBase.ErrorHandling | ||
| .ErrorCode.Message with NationalInstruments.SystemLink.ServiceBase.HttpError.Args inserted into | ||
| any placeholders.""" | ||
|
|
||
| resourceType: Optional[str] = None | ||
| """Gets the type of resource associated with the error, if any. Typically only used for instances within | ||
| NationalInstruments.SystemLink.ServiceBase.HttpError.InnerErrors.""" | ||
|
|
||
| resourceId: Optional[str] = None | ||
| """Gets the ID of the resource associated with the error, if any. Typically only used for instances within | ||
| NationalInstruments.SystemLink.ServiceBase.HttpError.InnerErrors.""" | ||
|
|
||
| args: Optional[List[str]] = None | ||
| """Gets the arguments that produced NationalInstruments.SystemLink.ServiceBase.HttpError.Message.""" | ||
|
|
||
| innerErrors: Optional[List["HttpError"]] = None | ||
| """Gets any nested errors if this instance represents more than one error. Typically only set for instances | ||
| related to NationalInstruments.SystemLink.ServiceBase.ErrorHandling.SkylineErrorCodes.OneOrMoreErrorsOccurred.""" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skipping the suggestion as list jobs are not paged as it don't have continuation token property.