Skip to content

Commit

Permalink
Update runners command (nmklotas#161)
Browse files Browse the repository at this point in the history
Co-authored-by: Joseph Petersen <[email protected]>
  • Loading branch information
sandercox and jetersen authored Sep 2, 2020
1 parent 053d1a3 commit 30f6242
Show file tree
Hide file tree
Showing 17 changed files with 574 additions and 18 deletions.
31 changes: 31 additions & 0 deletions docker/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,35 @@
t = PersonalAccessToken.new({ user: u, name: 'gitlab-api-client', scopes: ['api']})
t.save!

attributes = {
description: 'txxxestrunnexxxr',
active: true,
locked: false,
run_untagged: true,
tag_list: [ 'test' ],
runner_type: :instance_type
}
r = Ci::Runner::create!(attributes)
attributes = {
description: 'txxxestrunnexxxr_group',
active: true,
locked: false,
run_untagged: true,
tag_list: [ 'test' ],
runner_type: :group_type,
groups: [ g ]
}
r = Ci::Runner::create!(attributes)
attributes = {
description: 'txxxestrunnexxxr_project',
active: true,
locked: false,
run_untagged: true,
tag_list: [ 'test' ],
runner_type: :project_type,
projects: [ p ]
}
r = Ci::Runner::create!(attributes)

puts t.token
puts Gitlab::CurrentSettings.current_application_settings.runners_registration_token
8 changes: 8 additions & 0 deletions src/GitLabApiClient/GroupsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using GitLabApiClient.Models.Milestones.Requests;
using GitLabApiClient.Models.Milestones.Responses;
using GitLabApiClient.Models.Projects.Responses;
using GitLabApiClient.Models.Runners.Responses;

namespace GitLabApiClient
{
Expand Down Expand Up @@ -165,6 +166,13 @@ public async Task<IList<Milestone>> GetMilestonesAsync(GroupId groupId, Action<M
public async Task<Milestone> GetMilestoneAsync(GroupId groupId, int milestoneId) =>
await _httpFacade.Get<Milestone>($"groups/{groupId}/milestones/{milestoneId}");

/// <summary>
/// Get a list of runners in a group.
/// </summary>
/// <param name="groupId">The ID, path or <see cref="Group"/> of the group.</param>
public async Task<IList<Runner>> GetRunnersAsync(GroupId groupId) =>
await _httpFacade.Get<IList<Runner>>($"groups/{groupId}/runners");

/// <summary>
/// Creates a new project group.
/// Available only for users who can create groups.
Expand Down
8 changes: 8 additions & 0 deletions src/GitLabApiClient/IGroupsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using GitLabApiClient.Models.Milestones.Requests;
using GitLabApiClient.Models.Milestones.Responses;
using GitLabApiClient.Models.Projects.Responses;
using GitLabApiClient.Models.Runners.Requests;
using GitLabApiClient.Models.Runners.Responses;

namespace GitLabApiClient
{
Expand Down Expand Up @@ -87,6 +89,12 @@ public interface IGroupsClient
/// <param name="milestoneId">Id of the milestone.</param>
Task<Milestone> GetMilestoneAsync(GroupId groupId, int milestoneId);

/// <summary>
/// Get a list of runners in a group.
/// </summary>
/// <param name="groupId">The ID, path or <see cref="Group"/> of the group.</param>
Task<IList<Runner>> GetRunnersAsync(GroupId groupId);

/// <summary>
/// Creates a new project group.
/// Available only for users who can create groups.
Expand Down
42 changes: 33 additions & 9 deletions src/GitLabApiClient/IRunnersClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GitLabApiClient.Models.Runners.Requests;
using GitLabApiClient.Models.Runners.Responses;
using GitLabApiClient.Models.Users.Requests;
using GitLabApiClient.Models.Users.Responses;

namespace GitLabApiClient
{
Expand All @@ -14,24 +13,49 @@ public interface IRunnersClient
Task<IList<Runner>> GetAsync();

/// <summary>
/// Retrieves an user matched by name.
/// Retrieves all registered runners.
/// </summary>
Task<IList<Runner>> GetAllAsync();

/// <summary>
/// Retrieves a runner matched by id.
/// </summary>
/// <param name="runnerId">Id of the runner.</param>
/// <returns>Runner or NULL if it was not found.</returns>
Task<Runner> GetAsync(int runnerId);
Task<RunnerDetails> GetAsync(int runnerId);

/// <summary>
/// Updates existing user
/// Updates existing runner
/// </summary>
/// <param name="runnerId">Id of the runner.</param>
/// <param name="request">Request to update user.</param>
/// <returns>Newly modified user.</returns>
Task<User> UpdateAsync(int runnerId, UpdateUserRequest request);
/// <param name="request">Request to update runner.</param>
/// <returns>Newly modified runner.</returns>
Task<Runner> UpdateAsync(int runnerId, UpdateRunnerRequest request);

/// <summary>
/// Creates a new runner registration.
/// </summary>
/// <returns>The newly created runner.</returns>
/// <param name="request">Create runner request.</param>
Task<RunnerToken> CreateAsync(CreateRunnerRequest request);

/// <summary>
/// Deletes user.
/// Deletes a runner.
/// </summary>
/// <param name="runnerId">Id of the runner.</param>
Task DeleteAsync(int runnerId);

/// <summary>
/// Deletes a runner.
/// </summary>
/// <param name="runnerToken">Token of the runner.</param>
Task DeleteAsync(string runnerToken);

/// <summary>
/// Checks if a runner token can authenticate with GitLab
/// </summary>
/// <returns>True is token is valid, false if not valid.</returns>
/// <param name="token">Token to check.</param>
Task<bool> VerifyAuthenticationAsync(string token);
}
}
2 changes: 2 additions & 0 deletions src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public Task Put(string uri, object data) =>

public Task Delete(string uri) =>
_requestor.Delete(uri);
public Task Delete(string uri, object data) =>
_requestor.Delete(uri, data);

public async Task<AccessTokenResponse> LoginAsync(AccessTokenRequest accessTokenRequest)
{
Expand Down
10 changes: 10 additions & 0 deletions src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public async Task Delete(string url)
await EnsureSuccessStatusCode(responseMessage);
}

public async Task Delete(string url, object data)
{
var request = new HttpRequestMessage(HttpMethod.Delete, url)
{
Content = SerializeToString(data)
};
var responseMessage = await _client.SendAsync(request);
await EnsureSuccessStatusCode(responseMessage);
}

public async Task<Tuple<T, HttpResponseHeaders>> GetWithHeaders<T>(string url)
{
var responseMessage = await _client.GetAsync(url);
Expand Down
61 changes: 61 additions & 0 deletions src/GitLabApiClient/Models/Runners/Requests/CreateRunnerRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Runners.Requests
{
/// <summary>
/// Registers a new runner with gitlab.
/// </summary>
public sealed class CreateRunnerRequest
{
/// <summary>
/// GitLab token to register runner
/// </summary>
[JsonProperty("token")]
public string Token { get; set; }

/// <summary>
/// The description of a runner.
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// The state of a runner; can be set to true or false.
/// </summary>
[JsonProperty("active")]
public bool? Active { get; set; }

/// <summary>
/// Flag indicating the runner is locked.
/// </summary>
[JsonProperty("locked")]
public bool? Locked { get; set; }

/// <summary>
/// Flag indicating the runner can execute untagged jobs.
/// </summary>
[JsonProperty("run_untagged")]
public bool? RunUntagged { get; set; }

/// <summary>
/// The list of tags for a runner; put array of tags, that should be finally assigned to a runner.
/// </summary>
[JsonProperty("tag_list")]
public List<string> TagList { get; set; }

/// <summary>
/// The access_level of the runner; not_protected or ref_protected.
/// </summary>
[JsonProperty("access_level")]
public string AccessLevel { get; set; }

/// <summary>
/// Maximum timeout set when this Runner will handle the job.
/// </summary>
[JsonProperty("maximum_timeout")]
public int? MaximumTimeout { get; set; }
}
}
55 changes: 55 additions & 0 deletions src/GitLabApiClient/Models/Runners/Requests/UpdateRunnerRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Runners.Requests
{
/// <summary>
/// Modifies an existing user.
/// </summary>
public sealed class UpdateRunnerRequest
{
/// <summary>
/// The description of a runner.
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// The state of a runner; can be set to true or false.
/// </summary>
[JsonProperty("active")]
public bool? Active { get; set; }

/// <summary>
/// The list of tags for a runner; put array of tags, that should be finally assigned to a runner.
/// </summary>
[JsonProperty("tag_list")]
public List<string> TagList { get; set; }

/// <summary>
/// Flag indicating the runner can execute untagged jobs.
/// </summary>
[JsonProperty("run_untagged")]
public bool? RunUntagged { get; set; }

/// <summary>
/// Flag indicating the runner is locked.
/// </summary>
[JsonProperty("locked")]
public bool? Locked { get; set; }

/// <summary>
/// The access_level of the runner; not_protected or ref_protected.
/// </summary>
[JsonProperty("access_level")]
public string AccessLevel { get; set; }

/// <summary>
/// Maximum timeout set when this Runner will handle the job.
/// </summary>
[JsonProperty("maximum_timeout")]
public int? MaximumTimeout { get; set; }
}
}
60 changes: 60 additions & 0 deletions src/GitLabApiClient/Models/Runners/Responses/RunnerDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Runners.Responses
{
public sealed class RunnerDetails
{
[JsonProperty("active")]
public bool Active { get; set; }

[JsonProperty("architecture")]
public string Architecture { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("ip_address")]
public string IpAddresses { get; set; }

[JsonProperty("is_shared")]
public bool IsShared { get; set; }

[JsonProperty("contacted_at")]
public string ContactedAt { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("online")]
public bool Online { get; set; }

[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("platform")]
public string Platform { get; set; }

[JsonProperty("projects")]
public List<RunnerProject> Projects { get; } = new List<RunnerProject>();

[JsonProperty("revision")]
public string Revision { get; set; }

[JsonProperty("tag_list")]
public List<string> TagList { get; } = new List<string>();

[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("access_level")]
public string AccessLevel { get; set; }

[JsonProperty("maximum_timeout")]
public int MaximumTimeout { get; set; }
}

}
23 changes: 23 additions & 0 deletions src/GitLabApiClient/Models/Runners/Responses/RunnerProject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Runners.Responses
{
public sealed class RunnerProject
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("name_with_namespace")]
public string NameWithNamespace { get; set; }

[JsonProperty("path")]
public string Path { get; set; }

[JsonProperty("path_with_namespace")]
public string PathWithNamespace { get; set; }
}

}
13 changes: 13 additions & 0 deletions src/GitLabApiClient/Models/Runners/Responses/RunnerToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Runners.Responses
{
public sealed class RunnerToken
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("token")]
public string Token { get; set; }
}
}
Loading

0 comments on commit 30f6242

Please sign in to comment.