-
Notifications
You must be signed in to change notification settings - Fork 32
Refactor LinkIndexProvider into own project #1302
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
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Elastic.Documentation.LinkIndex/Elastic.Documentation.LinkIndex.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="AWSSDK.S3" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Elastic.Documentation\Elastic.Documentation.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Net; | ||
| using Amazon.Runtime; | ||
| using Amazon.S3; | ||
| using Amazon.S3.Model; | ||
| using Elastic.Documentation.Links; | ||
|
|
||
| namespace Elastic.Documentation.LinkIndex; | ||
|
|
||
| public interface ILinkIndexProvider | ||
| { | ||
| Task<LinkReferenceRegistry> GetLinkIndex(Cancel cancellationToken = default); | ||
reakaleek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Task SaveLinkIndex(LinkReferenceRegistry registry, Cancel cancellationToken = default); | ||
| Task<LinkReference> GetLinkReference(string key, Cancel cancellationToken = default); | ||
| string GetLinkIndexPublicUrl(); | ||
reakaleek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public class AwsS3LinkIndexProvider(IAmazonS3 s3Client, string bucketName = "elastic-docs-link-index", string registryKey = "link-index.json") : ILinkIndexProvider | ||
| { | ||
|
|
||
| // <summary> | ||
| // Using <see cref="AnonymousAWSCredentials"/> to access the link index | ||
| // allows to read from the link index without the need to provide AWS credentials. | ||
| // </summary> | ||
| public static AwsS3LinkIndexProvider CreateAnonymous() | ||
| { | ||
| var credentials = new AnonymousAWSCredentials(); | ||
| var config = new AmazonS3Config | ||
| { | ||
| RegionEndpoint = Amazon.RegionEndpoint.USEast2 | ||
| }; | ||
| var s3Client = new AmazonS3Client(credentials, config); | ||
| return new AwsS3LinkIndexProvider(s3Client); | ||
| } | ||
|
|
||
| public async Task<LinkReferenceRegistry> GetLinkIndex(Cancel cancellationToken = default) | ||
| { | ||
| var getObjectRequest = new GetObjectRequest | ||
| { | ||
| BucketName = bucketName, | ||
| Key = registryKey | ||
| }; | ||
| var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken); | ||
| await using var stream = getObjectResponse.ResponseStream; | ||
| var linkIndex = LinkReferenceRegistry.Deserialize(stream); | ||
| return linkIndex with { ETag = getObjectResponse.ETag }; | ||
| } | ||
|
|
||
| public async Task SaveLinkIndex(LinkReferenceRegistry registry, Cancel cancellationToken = default) | ||
| { | ||
| if (registry.ETag == null) | ||
| // The ETag should not be null if the LinkReferenceRegistry was retrieved from GetLinkIndex() | ||
| throw new InvalidOperationException($"{nameof(LinkReferenceRegistry)}.{nameof(registry.ETag)} cannot be null"); | ||
| var json = LinkReferenceRegistry.Serialize(registry); | ||
| var putObjectRequest = new PutObjectRequest | ||
| { | ||
| BucketName = bucketName, | ||
| Key = registryKey, | ||
| ContentBody = json, | ||
| ContentType = "application/json", | ||
| IfMatch = registry.ETag // Only update if the ETag matches. Meaning the object has not been changed in the meantime. | ||
| }; | ||
| var putResponse = await s3Client.PutObjectAsync(putObjectRequest, cancellationToken); | ||
| if (putResponse.HttpStatusCode != HttpStatusCode.OK) | ||
| throw new Exception($"Unable to save {nameof(LinkReferenceRegistry)} to s3://{bucketName}/{registryKey}"); | ||
| } | ||
|
|
||
| public async Task<LinkReference> GetLinkReference(string key, Cancel cancellationToken) | ||
| { | ||
| var getObjectRequest = new GetObjectRequest | ||
| { | ||
| BucketName = bucketName, | ||
| Key = key | ||
| }; | ||
| var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken); | ||
| await using var stream = getObjectResponse.ResponseStream; | ||
| return LinkReference.Deserialize(stream); | ||
| } | ||
|
|
||
| public string GetLinkIndexPublicUrl() => $"https://{bucketName}.s3.{s3Client.Config.RegionEndpoint.SystemName}.amazonaws.com/{registryKey}"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.