Skip to content

Instantly share code, notes, and snippets.

@GeorgeTsiokos
Created March 26, 2019 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgeTsiokos/55e058e1f32257d6a0d3e94d0ee59855 to your computer and use it in GitHub Desktop.
Save GeorgeTsiokos/55e058e1f32257d6a0d3e94d0ee59855 to your computer and use it in GitHub Desktop.
Rancher in ASP.NET Core
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<IRancherClient, RancherClient>();
}
public interface IRancherClient
{
Task<HttpResponseMessage> ProjectStackActionAsync(string project, string stack, string action,
HttpContent content = null);
Task<HttpResponseMessage> ProjectServiceActionAsync(string project, string service, string action,
HttpContent content = null);
}
public sealed class RancherClient : IRancherClient
{
private readonly StringContent _emptyContent = new ImmutableStringContent("{}");
private readonly HttpClient _httpClient;
public RancherClient(HttpClient httpClient) => _httpClient = Configure(httpClient);
public async Task<HttpResponseMessage> ProjectStackActionAsync(string project, string stack, string action,
HttpContent content = null)
{
using (var httpRequestMessage = new HttpRequestMessage(
HttpMethod.Post,
$"/v2-beta/projects/{project}/stacks/{stack}/?action={action}")
{
Content = content ?? _emptyContent
})
{
return await _httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
}
}
public async Task<HttpResponseMessage> ProjectServiceActionAsync(string project, string service, string action,
HttpContent content = null)
{
using (var httpRequestMessage = new HttpRequestMessage(
HttpMethod.Post,
$"/v2-beta/projects/{project}/services/{service}/?action={action}")
{
Content = content ?? _emptyContent
})
{
return await _httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
}
}
private static HttpClient Configure(HttpClient httpClient)
{
var url = Environment.GetEnvironmentVariable("CATTLE_URL");
if (null == url)
return httpClient;
var uriBuilder = new UriBuilder(url) {Path = null};
httpClient.BaseAddress = uriBuilder.Uri;
var authString = Environment.GetEnvironmentVariable("CATTLE_AGENT_INSTANCE_AUTH");
if (null == authString)
return httpClient;
httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authString);
return httpClient;
}
}
/// <inheritdoc />
/// <summary>
/// StringContent that can be re-used.
/// </summary>
/// <remarks>Use for constants</remarks>
public sealed class ImmutableStringContent : StringContent
{
public ImmutableStringContent(string content) : base(content)
{
}
public ImmutableStringContent(string content, Encoding encoding) : base(content, encoding)
{
}
public ImmutableStringContent(string content, Encoding encoding, string mediaType) : base(
content,
encoding,
mediaType)
{
}
protected override void Dispose(bool disposing)
{
// don't do anything
}
}
@GeorgeTsiokos
Copy link
Author

GeorgeTsiokos commented Mar 26, 2019

Usage:

var project = "1a50305";
var service = "1s10124";
var action = "deactivate";
var response = await _rancherClient.ProjectServiceActionAsync(project, service, action);

@GeorgeTsiokos
Copy link
Author

If you create a service that needs to interact with the Rancher API, service account API keys will need to be created for the containers so that the service will be able to access the API for authenticated set ups

https://rancher.com/docs/rancher/v1.6/en/rancher-services/service-accounts/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment