Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apollo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
apollo
Commits
152b0323
Commit
152b0323
authored
Dec 16, 2018
by
Jared Tan
Committed by
Jason Song
Dec 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:import and export configs. (#1767)
feat:import and export configs
parent
e4aa9f7f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
146 additions
and
0 deletions
+146
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java
...ork/apollo/portal/controller/ConfigsExportController.java
+116
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/ConfigToFileUtils.java
...ctrip/framework/apollo/portal/util/ConfigToFileUtils.java
+30
-0
No files found.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java
0 → 100644
View file @
152b0323
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
controller
;
import
com.ctrip.framework.apollo.common.dto.NamespaceDTO
;
import
com.ctrip.framework.apollo.common.exception.BadRequestException
;
import
com.ctrip.framework.apollo.common.exception.ServiceException
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO
;
import
com.ctrip.framework.apollo.portal.entity.model.NamespaceTextModel
;
import
com.ctrip.framework.apollo.portal.service.ItemService
;
import
com.ctrip.framework.apollo.portal.service.NamespaceService
;
import
com.ctrip.framework.apollo.portal.util.ConfigToFileUtils
;
import
com.google.common.base.Joiner
;
import
com.google.common.base.Splitter
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.stream.Collectors
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* jian.tan
*/
@RestController
public
class
ConfigsExportController
{
@Autowired
private
ItemService
configService
;
@Autowired
private
NamespaceService
namespaceService
;
@RequestMapping
(
value
=
"/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/import"
,
method
=
RequestMethod
.
POST
)
public
void
importConfigFile
(
@PathVariable
String
appId
,
@PathVariable
String
env
,
@PathVariable
String
clusterName
,
@PathVariable
String
namespaceName
,
@RequestParam
Integer
namespaceId
,
@RequestParam
(
"file"
)
MultipartFile
file
)
{
if
(
file
.
isEmpty
())
{
throw
new
BadRequestException
(
"The file is empty."
);
}
NamespaceDTO
namespaceDTO
=
namespaceService
.
loadNamespaceBaseInfo
(
appId
,
Env
.
fromString
(
env
),
clusterName
,
namespaceName
);
if
(
Objects
.
isNull
(
namespaceDTO
))
{
throw
new
BadRequestException
(
String
.
format
(
"Namespace: {} not exist."
,
namespaceName
));
}
NamespaceTextModel
model
=
new
NamespaceTextModel
();
List
<
String
>
fileNameSplit
=
Splitter
.
on
(
"."
).
splitToList
(
file
.
getOriginalFilename
());
if
(
fileNameSplit
.
size
()
<=
1
)
{
throw
new
BadRequestException
(
"The file format is invalid."
);
}
String
format
=
fileNameSplit
.
get
(
fileNameSplit
.
size
()
-
1
);
model
.
setFormat
(
format
);
model
.
setAppId
(
appId
);
model
.
setEnv
(
env
);
model
.
setClusterName
(
clusterName
);
model
.
setNamespaceName
(
namespaceName
);
model
.
setNamespaceId
(
namespaceId
);
String
configText
;
try
(
InputStream
in
=
file
.
getInputStream
()){
configText
=
ConfigToFileUtils
.
fileToString
(
in
);
}
catch
(
IOException
e
)
{
throw
new
ServiceException
(
"Read config file errors:{}"
,
e
);
}
model
.
setConfigText
(
configText
);
configService
.
updateConfigItemByText
(
model
);
}
@RequestMapping
(
value
=
"/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/export"
,
method
=
RequestMethod
.
GET
)
public
void
exportItems
(
@PathVariable
String
appId
,
@PathVariable
String
env
,
@PathVariable
String
clusterName
,
@PathVariable
String
namespaceName
,
HttpServletResponse
res
)
{
List
<
String
>
fileNameSplit
=
Splitter
.
on
(
"."
).
splitToList
(
namespaceName
);
String
fileName
=
fileNameSplit
.
size
()
<=
1
?
Joiner
.
on
(
"."
)
.
join
(
namespaceName
,
ConfigFileFormat
.
Properties
.
getValue
())
:
namespaceName
;
NamespaceBO
namespaceBO
=
namespaceService
.
loadNamespaceBO
(
appId
,
Env
.
fromString
(
env
),
clusterName
,
namespaceName
);
//generate a file.
res
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
fileName
);
List
<
String
>
fileItems
=
namespaceBO
.
getItems
().
stream
().
map
(
itemBO
->
{
String
key
=
itemBO
.
getItem
().
getKey
();
String
value
=
itemBO
.
getItem
().
getValue
();
if
(
ConfigConsts
.
CONFIG_FILE_CONTENT_KEY
.
equals
(
key
))
{
return
value
;
}
if
(
""
.
equals
(
key
))
{
return
Joiner
.
on
(
""
).
join
(
itemBO
.
getItem
().
getKey
(),
itemBO
.
getItem
().
getValue
());
}
return
Joiner
.
on
(
" = "
).
join
(
itemBO
.
getItem
().
getKey
(),
itemBO
.
getItem
().
getValue
());
}).
collect
(
Collectors
.
toList
());
try
{
ConfigToFileUtils
.
itemsToFile
(
res
.
getOutputStream
(),
fileItems
);
}
catch
(
Exception
e
)
{
throw
new
ServiceException
(
"export items failed:{}"
,
e
);
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/ConfigToFileUtils.java
0 → 100644
View file @
152b0323
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
util
;
import
java.io.BufferedReader
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.PrintWriter
;
import
java.util.List
;
import
java.util.stream.Collectors
;
/**
* jian.tan
*/
public
class
ConfigToFileUtils
{
public
static
void
itemsToFile
(
OutputStream
os
,
List
<
String
>
items
)
{
try
{
PrintWriter
printWriter
=
new
PrintWriter
(
os
);
items
.
forEach
(
item
->
printWriter
.
println
(
item
));
printWriter
.
close
();
}
catch
(
Exception
e
)
{
throw
e
;
}
}
public
static
String
fileToString
(
InputStream
inputStream
)
{
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
return
bufferedReader
.
lines
().
collect
(
Collectors
.
joining
(
System
.
lineSeparator
()));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment