Skip to content

Commit 74ea29e

Browse files
committed
https://github.com/optdyn/hub-client-java/issues/21
added uploadFile method to allow upload of raw files to Kurjun
1 parent 5d900c1 commit 74ea29e

File tree

5 files changed

+113
-43
lines changed

5 files changed

+113
-43
lines changed

src/main/java/io/subutai/client/api/HubClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ public String getCdnPrefix()
130130
*/
131131
List<Template> getTemplates();
132132

133+
/**
134+
* Uploads a file to Kurjun RAW category
135+
*
136+
* @param filename full path to file
137+
* @param version optional version of file
138+
*/
139+
void uploadFile( String filename, String version );
140+
133141
/**
134142
* Creates environment creation request object that should be populated further by calling party.
135143
*

src/main/java/io/subutai/client/impl/HubClientImplementation.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,18 @@ public class HubClientImplementation implements HubClient
108108
{
109109
this( hubEnv );
110110

111-
Preconditions.checkArgument( !isBlank( pgpKeyFilePath ) );
111+
Preconditions.checkArgument( !StringUtil.isBlank( pgpKeyFilePath ) );
112112

113113
this.pgpKeyPassword = Strings.isNullOrEmpty( pgpKeyPassword ) ? "" : pgpKeyPassword;
114114

115115
loadSecretKey( pgpKeyFilePath );
116116
}
117117

118118

119-
private boolean isBlank( String str )
120-
{
121-
return Strings.nullToEmpty( str ).trim().isEmpty();
122-
}
123-
124-
125119
public void login( final String username, final String password )
126120
{
127-
Preconditions.checkArgument( !isBlank( username ) );
128-
Preconditions.checkArgument( !isBlank( password ) );
121+
Preconditions.checkArgument( !StringUtil.isBlank( username ) );
122+
Preconditions.checkArgument( !StringUtil.isBlank( password ) );
129123

130124
HttpPost request =
131125
new HttpPost( String.format( "https://%s.subut.ai/rest/v1/client/login", hubEnv.getUrlPrefix() ) );
@@ -332,7 +326,7 @@ public List<Peer> getPublicPeers()
332326
@Override
333327
public void sharePeer( final String peerId, final long userId )
334328
{
335-
Preconditions.checkArgument( !isBlank( peerId ) );
329+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
336330
Preconditions.checkArgument( userId > 0 );
337331

338332
HttpPut request = new HttpPut(
@@ -356,7 +350,7 @@ public void sharePeer( final String peerId, final long userId )
356350
@Override
357351
public void unsharePeer( final String peerId, final long userId )
358352
{
359-
Preconditions.checkArgument( !isBlank( peerId ) );
353+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
360354
Preconditions.checkArgument( userId > 0 );
361355

362356
HttpDelete request = new HttpDelete(
@@ -380,7 +374,7 @@ public void unsharePeer( final String peerId, final long userId )
380374
@Override
381375
public void addPeerToFavorites( final String peerId )
382376
{
383-
Preconditions.checkArgument( !isBlank( peerId ) );
377+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
384378

385379
HttpPut request = new HttpPut(
386380
String.format( "https://%s.subut.ai/rest/v1/client/peers/favorite/%s", hubEnv.getUrlPrefix(),
@@ -403,7 +397,7 @@ public void addPeerToFavorites( final String peerId )
403397
@Override
404398
public void removePeerFromFavorites( final String peerId )
405399
{
406-
Preconditions.checkArgument( !isBlank( peerId ) );
400+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
407401

408402
HttpDelete request = new HttpDelete(
409403
String.format( "https://%s.subut.ai/rest/v1/client/peers/favorite/%s", hubEnv.getUrlPrefix(),
@@ -426,7 +420,7 @@ public void removePeerFromFavorites( final String peerId )
426420
@Override
427421
public void updatePeerScope( final String peerId, final Peer.Scope scope )
428422
{
429-
Preconditions.checkArgument( !isBlank( peerId ) );
423+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
430424
Preconditions.checkNotNull( scope );
431425

432426
HttpPut request = new HttpPut(
@@ -450,8 +444,8 @@ public void updatePeerScope( final String peerId, final Peer.Scope scope )
450444
@Override
451445
public void updatePeerName( final String peerId, final String name )
452446
{
453-
Preconditions.checkArgument( !isBlank( peerId ) );
454-
Preconditions.checkArgument( !isBlank( name ) );
447+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
448+
Preconditions.checkArgument( !StringUtil.isBlank( name ) );
455449

456450
CloseableHttpResponse response = null;
457451
try
@@ -480,7 +474,7 @@ public void updatePeerName( final String peerId, final String name )
480474
@Override
481475
public List<SshKey> getSshKeys( final String envId )
482476
{
483-
Preconditions.checkArgument( !isBlank( envId ) );
477+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
484478

485479
List<SshKey> sshKeys = Lists.newArrayList();
486480

@@ -510,8 +504,8 @@ public List<SshKey> getSshKeys( final String envId )
510504

511505
public void addSshKey( final String envId, final String sshKey )
512506
{
513-
Preconditions.checkArgument( !isBlank( envId ) );
514-
Preconditions.checkArgument( !isBlank( sshKey ) );
507+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
508+
Preconditions.checkArgument( !StringUtil.isBlank( sshKey ) );
515509

516510
HttpPost request = new HttpPost(
517511
String.format( "https://%s.subut.ai/rest/v1/client/environments/%s/ssh-keys/add", hubEnv.getUrlPrefix(),
@@ -537,8 +531,8 @@ public void addSshKey( final String envId, final String sshKey )
537531

538532
public void removeSshKey( final String envId, final String sshKey )
539533
{
540-
Preconditions.checkArgument( !isBlank( envId ) );
541-
Preconditions.checkArgument( !isBlank( sshKey ) );
534+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
535+
Preconditions.checkArgument( !StringUtil.isBlank( sshKey ) );
542536

543537
HttpPost request = new HttpPost(
544538
String.format( "https://%s.subut.ai/rest/v1/client/environments/%s/ssh-keys/remove",
@@ -564,8 +558,8 @@ public void removeSshKey( final String envId, final String sshKey )
564558

565559
public void startContainer( final String envId, final String contId )
566560
{
567-
Preconditions.checkArgument( !isBlank( envId ) );
568-
Preconditions.checkArgument( !isBlank( contId ) );
561+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
562+
Preconditions.checkArgument( !StringUtil.isBlank( contId ) );
569563

570564
HttpPut request = new HttpPut(
571565
String.format( "https://%s.subut.ai/rest/v1/client/environments/%s/containers/%s/start",
@@ -587,8 +581,8 @@ public void startContainer( final String envId, final String contId )
587581

588582
public void stopContainer( final String envId, final String contId )
589583
{
590-
Preconditions.checkArgument( !isBlank( envId ) );
591-
Preconditions.checkArgument( !isBlank( contId ) );
584+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
585+
Preconditions.checkArgument( !StringUtil.isBlank( contId ) );
592586

593587
HttpPut request = new HttpPut(
594588
String.format( "https://%s.subut.ai/rest/v1/client/environments/%s/containers/%s/stop",
@@ -610,8 +604,8 @@ public void stopContainer( final String envId, final String contId )
610604

611605
public void destroyContainer( final String envId, final String contId )
612606
{
613-
Preconditions.checkArgument( !isBlank( envId ) );
614-
Preconditions.checkArgument( !isBlank( contId ) );
607+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
608+
Preconditions.checkArgument( !StringUtil.isBlank( contId ) );
615609

616610
HttpDelete request = new HttpDelete(
617611
String.format( "https://%s.subut.ai/rest/v1/client/environments/%s/containers/%s",
@@ -633,7 +627,7 @@ public void destroyContainer( final String envId, final String contId )
633627

634628
public EnvironmentCreationRequest createRequest( final String environmentName )
635629
{
636-
Preconditions.checkArgument( !isBlank( environmentName ) );
630+
Preconditions.checkArgument( !StringUtil.isBlank( environmentName ) );
637631

638632
return new EnvironmentCreationRequestImpl( environmentName );
639633
}
@@ -654,7 +648,7 @@ public EnvironmentRef createEnvironment( final EnvironmentCreationRequest enviro
654648
{
655649
node.setTemplateName( getTemplateNameById( templates, node.getTemplateId() ) );
656650

657-
Preconditions.checkArgument( !isBlank( node.getTemplateName() ),
651+
Preconditions.checkArgument( !StringUtil.isBlank( node.getTemplateName() ),
658652
"Template not found by id " + node.getTemplateId() );
659653
}
660654
//WORKAROUND!!!
@@ -689,7 +683,7 @@ public EnvironmentRef createEnvironment( final EnvironmentCreationRequest enviro
689683

690684
public EnvironmentModificationRequest modifyRequest( final String envId )
691685
{
692-
Preconditions.checkArgument( !isBlank( envId ) );
686+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
693687

694688
return new EnvironmentModificationRequestImpl( envId );
695689
}
@@ -783,7 +777,7 @@ String getTemplateNameById( final List<Template> templates, final String templat
783777

784778
public void destroyEnvironment( final String envId )
785779
{
786-
Preconditions.checkArgument( !isBlank( envId ) );
780+
Preconditions.checkArgument( !StringUtil.isBlank( envId ) );
787781

788782
HttpDelete request = new HttpDelete(
789783
String.format( "https://%s.subut.ai/rest/v1/client/environments/%s", hubEnv.getUrlPrefix(), envId ) );
@@ -808,6 +802,12 @@ public List<Template> getTemplates()
808802
}
809803

810804

805+
public void uploadFile( String filename, String version )
806+
{
807+
kurjunClient.uploadFile( filename, version, getKurjunToken() );
808+
}
809+
810+
811811
public Double getBalance()
812812
{
813813
HttpGet request =
@@ -866,7 +866,7 @@ public User getUser( final long userId )
866866
@Override
867867
public User findUserByName( final String name )
868868
{
869-
Preconditions.checkArgument( !isBlank( name ) );
869+
Preconditions.checkArgument( !StringUtil.isBlank( name ) );
870870

871871
UserImpl user;
872872
CloseableHttpResponse response = null;
@@ -902,7 +902,7 @@ public User findUserByName( final String name )
902902
@Override
903903
public User findUserByEmail( final String email )
904904
{
905-
Preconditions.checkArgument( !isBlank( email ) );
905+
Preconditions.checkArgument( !StringUtil.isBlank( email ) );
906906

907907
UserImpl user;
908908
CloseableHttpResponse response = null;
@@ -970,7 +970,7 @@ public List<Peer> getUserPeers( final long userId )
970970
@Override
971971
public List<User> getPeerUsers( final String peerId )
972972
{
973-
Preconditions.checkArgument( !isBlank( peerId ) );
973+
Preconditions.checkArgument( !StringUtil.isBlank( peerId ) );
974974

975975
List<User> users = Lists.newArrayList();
976976

@@ -1099,7 +1099,7 @@ public List<Domain> getDomains()
10991099
@Override
11001100
public void reserveDomain( final String domainName )
11011101
{
1102-
Preconditions.checkArgument( !isBlank( domainName ) );
1102+
Preconditions.checkArgument( !StringUtil.isBlank( domainName ) );
11031103

11041104
CloseableHttpResponse response = null;
11051105
try
@@ -1128,7 +1128,7 @@ public void reserveDomain( final String domainName )
11281128
@Override
11291129
public void deleteDomain( final String domainName )
11301130
{
1131-
Preconditions.checkArgument( !isBlank( domainName ) );
1131+
Preconditions.checkArgument( !StringUtil.isBlank( domainName ) );
11321132

11331133
CloseableHttpResponse response = null;
11341134
try

src/main/java/io/subutai/client/impl/KurjunClient.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.subutai.client.impl;
22

33

4+
import java.io.File;
45
import java.util.List;
56

67
import org.apache.commons.io.IOUtils;
@@ -10,6 +11,7 @@
1011
import org.apache.http.client.methods.HttpGet;
1112
import org.apache.http.client.methods.HttpPost;
1213
import org.apache.http.client.methods.HttpRequestBase;
14+
import org.apache.http.entity.ContentType;
1315
import org.apache.http.entity.mime.HttpMultipartMode;
1416
import org.apache.http.entity.mime.MultipartEntityBuilder;
1517
import org.apache.http.impl.client.CloseableHttpClient;
@@ -60,6 +62,35 @@ List<Template> getTemplates( String token )
6062
}
6163

6264

65+
void uploadFile( final String filename, final String version, final String kurjunToken )
66+
{
67+
HttpPost post = new HttpPost( String.format( "%s/raw/upload", getKurjunBaseUrl() ) );
68+
CloseableHttpClient client = HttpClients.createDefault();
69+
try
70+
{
71+
File file = new File( filename );
72+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
73+
builder.setMode( HttpMultipartMode.BROWSER_COMPATIBLE );
74+
builder.addBinaryBody( "file", file, ContentType.DEFAULT_BINARY, file.getName() );
75+
builder.addTextBody( "token", kurjunToken, ContentType.DEFAULT_BINARY );
76+
if ( !StringUtil.isBlank( version ) )
77+
{
78+
builder.addTextBody( "version", version, ContentType.DEFAULT_BINARY );
79+
}
80+
HttpEntity entity = builder.build();
81+
post.setEntity( entity );
82+
83+
CloseableHttpResponse response = execute( client, post );
84+
85+
checkHttpStatus( response, HttpStatus.SC_OK, "upload file" );
86+
}
87+
finally
88+
{
89+
IOUtils.closeQuietly( client );
90+
}
91+
}
92+
93+
6394
private List<Template> getKurjunTemplates( String token )
6495
{
6596
List<Template> templates = Lists.newArrayList();
@@ -169,12 +200,6 @@ private String getKurjunBaseUrl()
169200
}
170201

171202

172-
String toJson( Object object )
173-
{
174-
return gson.toJson( object );
175-
}
176-
177-
178203
private <T> T parse( CloseableHttpResponse response, TypeToken<T> typeToken )
179204
{
180205
try
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.subutai.client.impl;
2+
3+
4+
import com.google.common.base.Strings;
5+
6+
7+
public class StringUtil
8+
{
9+
10+
public static boolean isBlank( String str )
11+
{
12+
return Strings.nullToEmpty( str ).trim().isEmpty();
13+
}
14+
}

src/test/java/io/subutai/client/impl/HubClientImplementationTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,18 @@ public void testCreateEnvironmentFromBlueprint() throws Exception
580580
}
581581

582582

583+
@Test
584+
public void testUploadFile() throws Exception
585+
{
586+
File keyFile = File.createTempFile( "test-keys", ".tmp" );
587+
Files.copy( SignerTest.getKeyFileAsStream(), keyFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
588+
589+
hubClient.uploadFile( keyFile.getPath(), "version" );
590+
591+
verify( kurjunClient ).uploadFile( keyFile.getPath(), "version", "" );
592+
}
593+
594+
583595
/******* Real tests *******/
584596

585597
private void prepare()
@@ -950,14 +962,25 @@ public void testRealGetEnvironments() throws Exception
950962
public void testRealGetTemplates() throws Exception
951963
{
952964
hubClient = ( HubClientImplementation ) HubClients
953-
.getClient( HubClient.HubEnv.DEV, "C:\\Users\\Dilshat\\Desktop\\dilshat.aliev_all.asc", "" );
965+
.getClient( HubClient.HubEnv.PROD, "C:\\Users\\saltanat\\Downloads\\test.d_all.asc", "" );
954966

955967
List<Template> templates = hubClient.getTemplates();
956968

957969
System.out.println( templates );
958970
}
959971

960972

973+
@Test
974+
@Ignore
975+
public void testRealUploadFile() throws Exception
976+
{
977+
hubClient = ( HubClientImplementation ) HubClients
978+
.getClient( HubClient.HubEnv.PROD, "C:\\Users\\saltanat\\Downloads\\test.d_all.asc", "" );
979+
980+
hubClient.uploadFile( "C:\\Users\\saltanat\\Desktop\\test-file.txt", "1.2.3" );
981+
}
982+
983+
961984
@Test
962985
@Ignore
963986
public void testRealCreateEnvironment() throws Exception

0 commit comments

Comments
 (0)